JS - 重定向并返回主页

时间:2018-02-28 11:24:59

标签: javascript jquery html redirect

我有一个问题,我想在Page1中创建一个脚本,并在X秒重定向到Page2之后,并在Page2内部返回到Page1,并使其变得不难,这是我的问题,从第2页返回后,我想重定向到Page3,并返回到Page1,当我从Page3重新定位时,我想重定向到Page4,每次重定向都是X秒。 我知道我可以使用元标记重定向

<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。

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

您需要一个有状态的解决方案。这是一个可能的解决方案:

// 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>

$File1 = Get-Content -Path C:\Users\User1\Temp\file1.ini
$File2 = Get-Content -Path C:\Users\User1\Temp\file2.ini

$Name1 = ($File1 | Select-String "name=").line
$Name2 = ($File2 | Select-String "name=").line

if ($Name1 -ne $Name2)
{
    $File1.replace($name1,$name2)
    Set-Content -Path C:\Users\User1\Temp\file1.ini -Value $File1
}

答案 1 :(得分:0)

page1.html:OnLoad检查下一页的url参数

mid"2","3"

page2.html:将下一个重定向页面作为参数

传递
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));
}

答案 2 :(得分:0)

我这样解决了:

<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>