时间延迟重定向?

时间:2012-03-26 18:01:33

标签: javascript jquery html 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>

UPDATE:当HTML5滑到部分块时页面仍在使用所有代码进行渲染时,这很奇怪,因此JS应该被click事件触发,然后运行基于的重定向计时器。

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

6 个答案:

答案 0 :(得分:67)

修改

  

我面临的问题是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.

});

尝试使用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.

答案 1 :(得分:57)

<meta http-equiv="refresh" content="2; url=http://example.com/" />

此处2是以秒为单位的延迟。

答案 2 :(得分:9)

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

答案 3 :(得分:4)

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

示例代码(延迟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 -->

答案 4 :(得分:3)

当您滑动到名为博客的“部分”时,请在此处包含此代码。

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

myLink是您的href的ID。

答案 5 :(得分:0)

您可以直接将此信息包含在您的支持中。它工作得很好。我希望它对你有用。 onclick =&#34; setTimeout(&#39; location.href = ../../dashboard.xhtml;&#39;,7000);&#34;