我的代码基于youtube(我正在试图弄清楚youtube如何工作)但是后退按钮有问题 - 我需要点击后退按钮2次才能工作,当我使用时我不能再使用前进按钮了
这是代码:
index.php:
<?php
if (isset($_GET['n']) && $_GET['n'] == "n"){
f1();
}else{
f2();
}
function f2(){
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
<script>
function t(a) {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
if ((xhr.status >= 200 && xhr.status <= 300) || xhr.status == 304) {
document.getElementById("div1").innerHTML = xhr.responseText;
document.title = a;
}
};
xhr.open("get", a, true);
xhr.send(null);
history.pushState({state:3}, a, a);
this.oldHash = window.location.href;
return false
}
function hashHandler(){
this.oldHash = window.location.href;
var that = this;
var detect = function(){
if(that.oldHash!=window.location.href){
t(window.location.href);
that.oldHash = window.location.href;
}
};
this.Check = setInterval(function(){ detect() }, 100);
}
hashHandler();
</script>
</head>
<body id="div1">
<a href="Test.php" onclick="return t('Test.php')">index</a>
</body>
</html>
<?php
}
function f1(){
?>
<a href="Test.php" onclick="return t('Test.php')">index</a>
<?php
}
Test.php:
<?php
if (isset($_GET['n']) && $_GET['n'] == "n"){
f1();
}else{
f2();
}
function f2(){
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<script>
function t(a) {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
if ((xhr.status >= 200 && xhr.status <= 300) || xhr.status == 304) {
document.getElementById("div1").innerHTML = xhr.responseText;
document.title = a;
}
};
xhr.open("get", a, true);
xhr.send(null);
history.pushState({state:3}, a, a);
this.oldHash = window.location.href;
return false
}
function hashHandler(){
this.oldHash = window.location.href;
var that = this;
var detect = function(){
if(that.oldHash!=window.location.href){
t(window.location.href);
that.oldHash = window.location.href;
}
};
this.Check = setInterval(function(){ detect() }, 100);
}
hashHandler();
</script>
</head>
<body id="div1">
<a href="index.php" onclick="return t('index.php')">Test</a>
</body>
</html>
<?php
}
function f1(){
?>
<a href="index.php" onclick="return t('index.php')">Test</a>
<?php
}
请将此代码放在两个单独的PHP文件中 谢谢。