简体   繁体   中英

Why redirect on my website only works for once?

On my website, I made a redirect page to redirect people according to their languages and browsers. But it only works once, when I visit my website again, it won't redirect.

HTML:

<html lang="zh-CN">
<head>
    <script>
        function redirect(){
            // redirect according to browsers
            var temp = window.navigator.userAgent.toUpperCase();
            var charHead = temp.indexOf('MSIE');
            var charLast = temp.indexOf(";", charHead);
            var ieVersion = temp.substring(charHead, charLast);
            ieVersion = ieVersion.split(' ')[1];
            if (ieVersion <= 8) {
                window.location.href="https://compat.windowsme.xyz:914";
            } 

            if (localStorage.getItem("visit") == null)
            {
                // redirect according to user language
                console.log("redirecting...")
                var language = navigator.language || navigator.browserLanguage; //for IE
                if (language == 'zh-CN'){
                    document.location.href = 'zh-index.html';
                }
                else if (language == 'zh-HK' || language == 'zh-TW'){
                    document.location.href = 'hant-index.html';
                }
                else {
                    document.location.href = 'en-index.html';
                }
            }

            localStorage.setItem("visit", new Date());
            redirect()
        }
    </script>
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>redirect</title>
</head>
<body onload="redirect()">
</body>
</html>

It's because on the second visit the item in LocalStorage is not null anymore. Why do you store this information? If you remove the if handler, it will work on every site visit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM