繁体   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