我有一个问题,我想在Page1中创建一个脚本,并在X秒重定向到Page2之后,并且在Page2内部返回到Page1,并且使它不难,这是我的问题,从第2页返回后,我想要重定向到Page3,并返回到Page1,当我从Page3重新定位时,我想重定向到Page4,每次重定向都是X秒。我知道我可以使用meta标签重定向
<meta http-equiv="refresh" content="X;
URL=Page2.com">
和Page2相同的标签返回到Page1,但我需要在Page1中做到这样的事情
<!-- First Redirect -->
<meta http-equiv="refresh" content="x; URL=Page2.com">
<!-- Second Redirect -->
<meta http-equiv="refresh" content="x+x; URL=Page3.com">
<!-- Third Redirect -->
<meta http-equiv="refresh" content="x+x+x; URL=Page4.com">
<!-- Fouth Redirect -->
<meta http-equiv="refresh" content="x+x+x+x; URL=Page5.com">
在脚本中有一些东西吗?因为它不起作用,总是将我重定向到Page1.com。
谢谢你的帮助!
你需要一个有状态的解决方案。这是一个可能的解决方案:
// PAGEX content where X = 2, 3, 4, 5...
<meta http-equiv="refresh" content="X; URL=page1.com">
和
// PAGE1 content:
<script>
let urls = JSON.parse(localStorage.getItem('urls') || '[-1]')
let urlList = ['page2.com', 'page3.com', 'page4.com']
if(urls[0] == -1) { // fresh start
localStorage.setItem('urls', JSON.stringify(urlList))
startRedirection(urlList[0])
} else if(urls.length == 0) { // all redirects done
console.log("Exhausted")
localStorage.removeItem('urls') // removes the key "urls" for a fresh start
} else {
const url = urls.shift() // get the first url in "urls" array and remove it from the array
localStorage.setItem('urls', JSON.stringify(urls)) // update the state
startRedirection(url) // redirect
}
function startRedirection(url) {
setTimeout(() => location.href = url, 2000)
}
</script>
page1.html:OnLoad检查下一页的url参数
function onLoad(){
var url_string = window.location.href;
var url = new URL(url_string);
//Set pageName and time from url or some default value.
var pageName = url.searchParams.get("pageName") !== null ?
url.searchParams.get("pageName") : 'page2.html';
var time = url.searchParams.get("time") !== null ? url.searchParams.get("time") : 1000;
setTimeout(function(){
window.location = pageName;
}, parseInt(time));
}
page2.html:将下一个重定向页面作为参数传递
function onLoad(){
window.location = 'page1.html?pageName=page3.html&time=2000'
}
我这样解决了:
<script>
let urls = JSON.parse(localStorage.getItem('urls') || '[-1]')
let urlList = ['page2.com', 'page3.com', 'page4.com']
if(urls[0] == -1) { // fresh start
localStorage.setItem('urls', JSON.stringify(urlList))
startRedirection('page1.com')
} else if(urls.length == 0) { // all redirects done
console.log("Exhausted")
localStorage.removeItem('urls') // removes the key "urls" for a fresh start
location.reload();
} else {
const url = urls.shift() // get the first url in "urls" array and remove it
from the array
localStorage.setItem('urls', JSON.stringify(urls)) // update the state
startRedirection(url) // redirect
}
function startRedirection(url) {
setTimeout(() => location.href = url, 2000)
}
</script>