时间延迟重定向?

SOL*_*UNE 37 html javascript jquery html5

我有一个网站,可以在我的HTML中滑动到一个名为博客的"部分".我希望用户只需查看页面一小段时间或2,然后重定向到我的博客引擎.

请建议延迟重定向?

如果我可以根据博客li的点击延迟时间重定向:

<li data-target="blog" data-target-activation="click" class="tile icon_add_favourite">
          <div>
            <h2>Blog</h2><h3>13</h3>
                <script type="text/JavaScript">
                &lt;!--
                    setTimeout("location.href = 'http://www.mysite.co.uk/blog';", 1500);
                --&gt;
                </script>
          </div>
        </li>
Run Code Online (Sandbox Code Playgroud)

更新:当HTML5滑动到部分块时,这仍然很奇怪,页面仍在使用所有代码进行渲染,因此JS应该由click事件触发,然后根据计时器运行重定向.

当弹出警报框时,解决方案在JS中工作.不知道为什么他们不在我的页面工作?

Sel*_*gam 73

编辑:

我面临的问题是HTML5全部在一个索引页面上.所以我需要定时器点击博客链接开始.

尝试在博客链接上的单击处理程序中调用setTimeout,

$('#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.

});
Run Code Online (Sandbox Code Playgroud)

尝试使用setTimeout如下功能,

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.
Run Code Online (Sandbox Code Playgroud)


Mar*_*lin 58

<meta http-equiv="refresh" content="2; url=http://example.com/" />
Run Code Online (Sandbox Code Playgroud)

2是延迟秒.

  • 对于甚至不需要启用javascript的解决方案的+1. (3认同)
  • 我面临的问题是 HTML5 都在一个索引页面上。所以我需要计时器在点击博客链接时开始。 (2认同)
  • 只是想指出W3C已经弃用了元刷新的使用:https://en.wikipedia.org/wiki/Meta_refresh应该使用JS中接受的答案中的解决方案. (2认同)

ash*_*med 9

 <script type="text/JavaScript">
      setTimeout("location.href = 'http://www.your_site.com';",1500);
 </script>
Run Code Online (Sandbox Code Playgroud)


Pat*_*lán 5

您可以使用 JavaScript 轻松创建定时重定向。但我建议你使用location.replace('url')而不是location.href。它可以防止浏览器将站点推送到历史记录中。我找到了这个 JavaScript 重定向工具。我想你可以用这个。

示例代码(延迟 5 秒):

<!-- 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 -->
Run Code Online (Sandbox Code Playgroud)