2013-06-20 39 views
1

所有而在此之前,我们使用这个realiable网站重定向HTML/JavaScript代码是否有可能运行JavaScript代码元刷新

<!DOCTYPE HTML> 
<html lang="en-US"> 
    <head> 
     <meta charset="UTF-8"> 
     <meta http-equiv="refresh" content="1;url=https://www.nosuchwebsite.com"> 
     <script type="text/javascript"> 
      window.location.href = "https://www.nosuchwebsite.com" 
     </script> 
     <title>Page Redirection</title> 
    </head> 
    <body> 
     <!-- Note: don't tell people to `click` the link, just tell them that it is a link. --> 
     If you are not redirected automatically, follow the <a href='https://www.nosuchwebsite.com'>nosuchwebsite</a> 
    </body> 
</html> 

现在,我们希望有谷歌Analytics跟踪代码执行重定向之前。

<script type="text/javascript"> 

    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-xxxxxxxx-x']); 
    _gaq.push(['_trackPageview']); 

    (function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
    })(); 

</script> 

我意识到,meta“refresh”可能比JavaScript跟踪代码早执行。我想知道,是否有什么办法可以让谷歌Analytics跟踪代码,元之前运行“刷新”

回答

0

简单地加大对元刷新命令的秒数,它会等待较长重定向到新的页面之前。这将允许您的JavaScript同时执行。

<meta http-equiv="refresh" content="1;url=https://www.nosuchwebsite.com"> 
            ^

可以

<meta http-equiv="refresh" content="3;url=https://www.nosuchwebsite.com"> 
+0

但是。它足够安全可靠吗?我们只是假设3秒后JavaScript会完成执行? –

+0

3秒是tooooooooooooooooooooo一个谷歌脚本加载,远远不够。但是,如果你注意到它不够,你当然可以增加持续时间 –

0

还是不要使用元刷新和使用JavaScript刷新,并具有元刷新在<noscript>块的好办法,当已禁用Javascript Analytics(分析)不会无论如何工作。

相关问题