我的网站使用脚本来浏览AJAX页面。主页的网址是这样的:http://example.com/#home
,实际主页位于pages/page_home.php
。在哈希中添加目录AJAX
如何在散列中添加目录?这是我的代码。
<?php
if(!$_POST['page']) die("0");
$page = $_POST['page'];
if(file_exists('pages/page_'.$page.'.php'))
echo file_get_contents('pages/page_'.$page.'.php');
else echo '<div class="box">404 - That page could not be found. <a href="#home">Click here</a> to return.</div>';
?>
我的动机这样做是为了使条件页面,以便当访问者点击了/#conditions
网址更改/#conditions/cookies
饼干链接是整洁的。
编辑:全码
var state = { 'page_id': 1, 'user_id': 5 };
var title = 'Hello World';
var url = '#conditions/cookies';
history.pushState(state, title, url);
var default_content="";
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
options.async = true;
});
$(document).ready(function(){
checkURL();
$('ul li a').click(function (e){
checkURL(this.hash);
});
//filling in the default content
default_content = $('#pageContent').html();
setInterval("checkURL()",250);
});
var lasturl="";
function checkURL(hash)
{
if(!hash) hash=window.location.hash;
if(hash != lasturl)
{
lasturl=hash;
// FIX - if we've used the history buttons to return to the homepage,
// fill the pageContent with the default_content
if(hash=="")
$('#pageContent').html(default_content);
else
loadPage(hash);
}
}
function loadPage(url) {
url=url.replace('#','');
$.post("load_page.php", page: location.hash.substring(1)), function() {
history.pushState(null, '', '#conditions/cookies');
});
$.ajax({
type: "POST",
url: "load_page.php",
data: location.hash,
dataType: "html",
success: function(msg){
if(parseInt(msg)!=0) {
$('#pageContent').html(msg);
}
}
});
}
呃...有点像''? –
@PraveenKumar我知道,但脚本不是为此而生。它目前只支持像'/#conditions'这样的链接,而不是'/#conditions/cookies'。 – Mia
然后,您需要通过将'location.hash'的值发送到PHP来更改您的脚本以发送AJAX查询。检查我的答案。 –