所以我希望当用户刷新页面时我的页面重定向到不同的页面。我希望用户在刷新页面时从 gettingstarted.html
重定向到 index.html
。
代码:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
//meta, seo stuff
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
//body stuff
//below is just a script that redirects to gettingstarted.html upon first visit
<script type="text/javascript">
function redirect(){
var thecookie = readCookie('doRedirect');
if(!thecookie){window.location = 'http://domain.tld/gettingstarted.html';
}}function createCookie(name,value,days){if (days){var date = new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires = "; expires="+date.toGMTString();}else var expires = "";document.cookie = name+"="+value+expires+"; path=/";}function readCookie(name){var nameEQ = name + "=";var ca = document.cookie.split(';');for(var i=0;i < ca.length;i++){var c = ca[i];while (c.charAt(0)==' ') c = c.substring(1,c.length);if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);}return null;}window.onload = function(){redirect();createCookie('doRedirect','true','999');}
</script>
</body>
入门.html
与 index.html
几乎相同
我尝试用谷歌搜索,但我不断收到“元刷新标签”,在这种情况下,这是没有用的。
最佳答案
这将满足您的要求,只需自定义 URL。
<script>
if (sessionStorage.getItem('beenhere') ==='1') {
window.location="http://www.example.com";
}
sessionStorage.setItem('beenhere', '1');
</script>
参见:
关于javascript - 刷新时重定向到不同的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37265673/