How to make JS page redirect trigger before page load

后端 未结 2 1755
生来不讨喜
生来不讨喜 2021-01-15 21:13

I am using the following script to redirect visitors of a page to another page on first visit, however it loads the index.html page first, and then triggers the redirect. Ca

相关标签:
2条回答
  • 2021-01-15 21:56

    You don't need to wait while window is loaded:

    <script type="text/javascript">
        var thecookie = readCookie('doRedirect');
        if(!thecookie) {
           createCookie('doRedirect','true','1');
           window.location = '/coming-soon.html';
        };
        function createCookie(name,value,days){
          // do work
        }
        function readCookie(name){
          // do work
        }
    </script>
    

    Also Petr B. said right thing: server-side redirect is better in your case.

    0 讨论(0)
  • 2021-01-15 21:59

    Try this How to Run a jQuery or JavaScript Before Page Start to Load.

    Btw. if you want redirect without displaying page you must use php with cookies check.

    0 讨论(0)
提交回复
热议问题