假设:我点击1.php然后点击2.php然后点击3.php然后按回来但它不会转到2.php 网址也不受影响。我也想改变网址和支持,所以它可以是SEO友好的 这是我的代码:
<pre>
<html>
<head>
<script type="text/javascript">
function show(addr) {
a = new XMLHttpRequest();
a.onreadystatechange = function() {
if (a.readyState==4 && a.status==200) {
document.getElementById("content").innerHTML=a.responseText;
}
}
a.open("GET",addr+".php",true);
a.send(); //
}
</script>
</head>
<body>
<a href="#" onClick="show('1')">Load 1.php into content</a>
<a href="#" onClick="show('2')">Load 2.php into content</a>
<a href="#" onClick="show('3')">Load 3.php into content</a>
<a href="#" onClick="show('4')">Load 4.php into content</a>
<!-- Load content here-->
<div id="content"></div>
</body>
</html>
</pre>
答案 0 :(得分:1)
即使您的锚点具有唯一的标记名称,当您浏览浏览器历史记录并返回到该特定锚点时,它也不会执行与该锚点的onClick关联的javascript。
您需要设置window.onhashchange来调用show函数。更改后您甚至不需要onclick。我测试了以下内容:
<pre>
<html>
<head>
<script type="text/javascript">
window.onhashchange=window.onload=function(){
window.setTimeout(doHashCheck, 10);
}
var doHashCheck = (function(global) {
return function() {
var addr = window.location.hash.replace(/^#/,'');
show(addr);
}
})(this);
function show(addr)
{
a=new XMLHttpRequest();
a.onreadystatechange=function()
{
if (a.readyState==4 && a.status==200)
{
document.getElementById("content").innerHTML=a.responseText;
}
}
a.open("GET",addr+".php",true);
a.send(); //
}
</script>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache" />
<META HTTP-EQUIV="Expires" CONTENT="-1" />
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, no-store, must-revalidate" />
</head>
<a href="#1">Load 1.php into content</a>
<a href="#2">Load 2.php into content</a>
<a href="#3">Load 3.php into content</a>
<a href="#4">Load 4.php into content</a>
<!-- Load content here-->
<div id="content"></div>
</body>
</html>
</pre>