AJAX/Jquery/PHP-根据锚点/哈希标记加载页面
我有以下代码,非常有效:-AJAX/Jquery/PHP-根据锚点/哈希标记加载页面,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我有以下代码,非常有效:- $('.ajax a').click(function() { getpageajax(this.href); return false; }); function getpageajax(getpage) { if(getpage == "") { //SET ERROR? document.getElementById("main").innerHTML = ""; return;
$('.ajax a').click(function() {
getpageajax(this.href);
return false;
});
function getpageajax(getpage) {
if(getpage == "") {
//SET ERROR?
document.getElementById("main").innerHTML = "";
return;
}
if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("main").innerHTML = xmlhttp.responseText;
}
}
var urlarray = getpage.split('/');
location.hash = urlarray[urlarray.length-1];
xmlhttp.open("GET", "includes/AJAXgetpage.php?getpage=" + getpage, true);
xmlhttp.send();
};
唯一的问题是我不知道如何获取并阅读我创建的锚链接(例如#联系我们)来创建可书签页面
我试着根据以下几点做一个解决方案,但似乎不太理想
<script>
var query = location.href.split('#');
document.cookies = 'anchor=' + query[1];
<?php if (!$_COOKIE['anchor']) : ?>
window.location.reload();
<?php endif; ?>
<?php
echo $_COOKIE['anchor'];
?>
var query=location.href.split('#');
document.cookies='anchor='+查询[1];
window.location.reload();
要获取哈希值,除了最初的示例,您可以使用window.location.hash
// example url - http://www.mysite.com/blog/post#comments
var hash = window.location.hash; // = "#comments"
如果需要,可以使用
replace
删除前导的#
。要获取哈希值,除了初始示例之外,还可以使用window.location.hash
// example url - http://www.mysite.com/blog/post#comments
var hash = window.location.hash; // = "#comments"
如果需要,可以使用
replace
删除前导的#
。啊,当然,我只需要将锚传递给我已经拥有的函数。啊,当然,我只需要将锚传递给我已经拥有的函数。