简体   繁体   中英

Force JS to load on page load

i am trying to get a page to load using 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>

can anyone point me in the right direction? I can get function onload to work since its a popup window.

The load event fires when all the content on your page fully loads including the DOM (document object model) content, asynchronous javascript, frames and images.

You can also use the DOMContentLoaded event instead (faster) when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

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

The right answer...

The question is a bit misleading which seems to have started people down the wrong path. There is no need for a document onload handler. The code below works as expected without one.

This is an example I took from the GeoBytes site and reworked. The meta tag redirect part also works, but I've disabled it in the code posted here. The aim of the code is to automatically redirect users to a country specific page.

The GeoBytes site has many free web services and is worth a look.

Run the snippet to test:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM