我做了一個ajax函數調用一個/ pages文件夾中的php頁面,我打電話給我的頁面在ajax-container
div,但是當我點擊refresh the page
按鈕時,history pushstate正在工作,因爲我在地址欄中看到了頁面,但它僅加載了index.php的內容,但在我的ajax-container
中沒有任何反應。 那麼我怎麼能得到我刷新頁面時調用的頁面?Ajax頁面刷新頁面按鈕沒有返回點擊
htacces:
Options +FollowSymLinks
RewriteEngine On
RewriteBase/
RewriteRule ^([a-zA-Z0-9\-]*).php$ index.php?p=$1 [L]
Ajax的容器:
<div id="ajax-container">
<?php
$d = "pages/";
if (isset($_GET['p'])) {
$p = strtolower($_GET['p']);
if (preg_match("/^[a-z0-9\-]+$/", $p) && file_exists($d . $p . ".php")) {
include $d . $p . ".php";
} else {
include $d . "404.html";
}
} else {
include $d . "home.php";
}
?>
</div>
和我的AJAX功能:
var afficher = function(data, page) {
$('#ajax-container').delay(200).fadeOut(250, function() {
$('#ajax-container').empty();
$('#ajax-container').append(data);
$('#ajax-container').fadeIn(100, function() {});
});
};
var lastRequest = null;
if (lastRequest !== null) {
lastRequest.abort();
}
var loadPage = function(page, storeHistory) {
if (typeof storeHistory === 'undefined') {
storeHistory = true;
}
lastRequest = $.ajax({
url: "pages/" + page,
cache: false,
success: function(html) {
afficher(html, page);
if (storeHistory === true) {
history.pushState({
'key': 'value',
'url': page
}, '', page);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
afficher('erreur lors du chagement de la page');
}
});
return false;
};
window.addEventListener('load', function() {
setTimeout(function() {
window.addEventListener('popstate', function(e) {
if (e.state === null) {
loadPage('home.php');
} else {
loadPage(e['state']['url'], false);
}
});
}, 0);
});
嗯,我只是很熟悉純粹的JavaScript AJAX的頂部,你想要做什麼困惑。然而,「loadPage」看起來像你想要去一個新的頁面? ... AJAX不打算進入一個新的頁面,它帶來的內容,而無需重新加載頁面。 – Tyler
是的,我想做什麼它在我的index.php我帶'about.php'在我的ajax容器,所以現在在我的'about.php'我有另一個鏈接,我想當我點擊這個鏈接, AJAX用這個新頁面替換'about.php' – Tiaw