2013-06-20 45 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(分析)不會無論如何工作。

相關問題