Сообщение от Mateus
|
Можно, пожалуйста, поподробней?
|
примерно так:
<?php
header("Content-Type: application/javascript");
?>
$(function(){
// keep a reference to the query processor, so you can stop a query if necessary
var xhr;
function loadContent(url, push) {
// previous operation interrupt request
if (xhr) xhr.abort();
var
fragmentsUrl = url.split("?"),
reqUrl = fragmentsUrl.shift();
// ask for new data
xhr = $.ajax({
url: "<?php echo _LINK_PATH;?>ajax/core.php" + (fragmentsUrl.length ? "?" + fragmentsUrl.join("?") : ""),
data: {"action": "page", "url": reqUrl},
type: "post",
dataType: "json",
success: function(data, textStatus, xhr) {
if (data.status == 1) {
// change the content
$("#dynamic_content").html(data.page.template);
// iterate through all the tags with the name script
$("script").each(function(){
// looking for a modular script
if (/(.*)<?php echo str_replace('/', '\\/', _LINK_PATH );?>js\/jscore.php\?module(.*)$/i.test(this.src)) {
var parent = this.parentNode;
// remove the script found to free up memory
parent.removeChild( this );
// Create a new script
var script = document.createElement('script');
script.type = "text/javascript";
script.async = "async";
script.src = "<?php echo _LINK_PATH;?>js/jscore.php?module=" + data.page.module;
// load it for execution
parent.appendChild(script);
return false;
}
});
if (data.page.title) {
document.title = data.page.title;
}
if (push) {
// replace the link in your browser
history.pushState(null, null, url);
}
}
},
complete: function(xhr, textStatus) {
//alert(xhr.responseText);
},
error: function(xhr, textStatus) {
}
});
}
$(window).on('popstate', function(e){
/*
* mind you, this is the only difference when working with the library,
* Because the object document.location not be rebooted, so
* History library returns generated by "location" object inside
* Object window.history, so get it out of the "history.location".
* For browsers support "history.pushState" get
* Shaped object "location" with the usual "document.location".
*/
var loc = history.location || document.location;
// load the desired content
loadContent(loc.href);
});
$(document).on('click', 'a.ajax', function(e) {
if (window.history.pushState) {
var href = $(e.target).closest('a').attr('href');
href && loadContent(href, true);
e.preventDefault();
}
});
// if the link has a class of 'external' means to open it in another browser tab
$(document).on('click', 'a.external', function(e) {
var href = $(e.target).closest('a').attr('href');
href && window.open(this.href);
e.preventDefault();
});
});