time delayed redirect?

前端 未结 6 1481
花落未央
花落未央 2020-11-27 18:19

I have a website which slides to a \"section\" called blog in my HTML. I want the user to simply see the page for a brief second or 2 then redirect to my blog engine.

<
相关标签:
6条回答
  • 2020-11-27 18:46

    You can easily create timed redirections with JavaScript. But I suggest you to use location.replace('url') instead of location.href. It prevents to browser to push the site into the history. I found this JavaScript redirect tool. I think you could use this.

    Example code (with 5 secs delay):

    <!-- Pleace this snippet right after opening the head tag to make it work properly -->
    
    <!-- This code is licensed under GNU GPL v3 -->
    <!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
    <!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->
    
    <!-- REDIRECTING STARTS -->
    <link rel="canonical" href="https://yourdomain.com/"/>
    <noscript>
        <meta http-equiv="refresh" content="5;URL=https://yourdomain.com/">
    </noscript>
    <!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
    <script type="text/javascript">
        var url = "https://yourdomain.com/";
        var delay = "5000";
        window.onload = function ()
        {
            setTimeout(GoToURL, delay);
        }
        function GoToURL()
        {
            if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
            {
                var referLink = document.createElement("a");
                referLink.href = url;
                document.body.appendChild(referLink);
                referLink.click();
            }
            else { window.location.replace(url); } // All other browsers
        }
    </script>
    <!-- Credit goes to http://insider.zone/ -->
    <!-- REDIRECTING ENDS -->
    
    0 讨论(0)
  • 2020-11-27 18:47

    Edit:

    The problem i face is that HTML5 is all on one index page. So i need the timer to start on click of the blog link.

    Try calling the setTimeout inside a click handler on the blog link,

    $('#blogLink').click (function (e) {
       e.preventDefault(); //will stop the link href to call the blog page
    
       setTimeout(function () {
           window.location.href = "blog.html"; //will redirect to your blog page (an ex: blog.html)
        }, 2000); //will call the function after 2 secs.
    
    });
    

    Try using setTimeout function like below,

    setTimeout(function () {
       window.location.href = "blog.html"; //will redirect to your blog page (an ex: blog.html)
    }, 2000); //will call the function after 2 secs.
    
    0 讨论(0)
  • 2020-11-27 18:48

    You can include this directly in your buttun. It works very well. I hope it'll be useful for you. onclick="setTimeout('location.href = ../../dashboard.xhtml;', 7000);"

    0 讨论(0)
  • 2020-11-27 18:52

    Include this code somewhere when you slide to your 'section' called blog.

    $("#myLink").click(function() {
        setTimeout(function() {
            window.navigate("the url of the page you want to navigate back to");
        }, 2000);
    });
    

    Where myLink is the id of your href.

    0 讨论(0)
  • 2020-11-27 19:07
    <meta http-equiv="refresh" content="2; url=http://example.com/" />
    

    Here 2 is delay in seconds.

    0 讨论(0)
  • 2020-11-27 19:08
     <script type="text/JavaScript">
          setTimeout("location.href = 'http://www.your_site.com';",1500);
     </script>
    
    0 讨论(0)
提交回复
热议问题