簡體   English   中英

強制JS在頁面加載時加載

[英]Force JS to load on page load

我正在嘗試使用Geobytes加載頁面。

<html>
<head>
</head>
<body style="background: transparent; border: none;">
<script src="http://gd.geobytes.com/Gd?pages=US&ext=html&after=-1" language="Javascript"></script><script language="javascript">// <![CDATA[
if(typeof(sGeobytesLocationCode)!="undefined"&&sGeobytesLocationCode.indexOf('US')==0)
{
    document.write("<META HTTP-EQUIV='Refresh' CONTENT='06; URL=http://www.example.com'>");
}
// ]]></script>
</body>
</html>

誰能指出我正確的方向? 因為它有一個彈出窗口,所以我可以讓函數onload正常工作。

當頁面上的所有內容(包括DOM(文檔對象模型)內容,異步javascript,框架和圖像)完全加載時,將觸發load事件。

當初始HTML文檔已完全加載和解析時,也可以使用DOMContentLoaded事件(更快),而不必等待樣式表,圖像和子幀完成加載。

<script>
// For legacy browsers use this instead: window.onload = function()
window.addEventListener('load', function () {

  if(typeof(sGeobytesLocationCode)!="undefined" && sGeobytesLocationCode.indexOf('US')==0)
  {
    document.write("<META HTTP-EQUIV='Refresh' CONTENT='06; URL=http://www.example.com'>");
  }

}, false);
</script>
<html>
    <head>        
        <script src="http://gd.geobytes.com/Gd?pages=US&ext=html&after=-1" type="text/javascript"></script>
        <script>

            function after_load() {
                if (typeof (sGeobytesLocationCode) != "undefined" && sGeobytesLocationCode.indexOf('US') == 0)
                {
                    document.write("<META HTTP-EQUIV='Refresh' CONTENT='06; URL=http://www.example.com'>");
                }                
            }

        </script>
    </head>
    <body onload="after_load()" style="background: transparent; border: none;">
    </body>
</html>

正確的答案...

這個問題有點誤導,似乎使人們誤入歧途。 不需要文檔加載處理程序。 下面的代碼可以正常工作,而無需一個。

這是我從GeoBytes站點上提取並重新編寫的示例。 meta標簽重定向部分也可以使用,但是我已在此處發布的代碼中將其禁用。 該代碼的目的是自動將用戶重定向到特定國家/地區的頁面。

GeoBytes網站提供許多免費的Web服務,值得一看。

運行代碼片段進行測試:

 <html> <head> <script src="http://gd.geobytes.com/Gd?pages=US&ext=html&after=-1" type="text/javascript"></script> <script type="text/javascript"> // DISABLED: Redirect vistors from USA to www.whitehouse.gov by inserting a meta tag if(typeof(sGeobytesLocationCode)!="undefined" && sGeobytesLocationCode.indexOf('US')==0) { // document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=https:\\\\www.whitehouse.gov'>"); } </script> </head> <body> <a href="http://www.geobytes.com/geodirection/" target="_blank">GeoBytes API</a> <p> <script type="text/javascript"> document.write('sGeobytesLocationCode = ' + sGeobytesLocationCode ); </script> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM