Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13

Thread: Script Load

  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Script Load

    I want this script to load after the page load
    the script is:
    Code:
    <script type="text/javascript" src="http://localtimes.info/clock.php?cp3_Hex=0F0200&cp2_Hex=FFFFFF&cp1_Hex=000080&fwdt=118&ham=0&hbg=0&hfg=0&sid=&mon=&wek=&wkf=&sep=&continent=Asia&country=Indonesia&city=Jakarta&widget_number=116"></script>
    the script in My page is like:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>My test page</title>
    </head>
    
    <body>
    <h1>Tony</h1>
    <h2>Welcome Tony Website</h2>
    <script type="text/javascript" src="http://localtimes.info/clock.php?cp3_Hex=0F0200&cp2_Hex=FFFFFF&cp1_Hex=000080&fwdt=118&ham=0&hbg=0&hfg=0&sid=&mon=&wek=&wkf=&sep=&continent=Asia&country=Indonesia&city=Jakarta&widget_number=116"></script>
    <h3>Tony is ... and is....</h3>
    <h3>He will....</h3>
    </body>
    
    </html>
    So how can I make this script loads after all elements of My page are loaded and without changing it's position (the script position)?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,894
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    You can only load the script last of all by placing it right ahead of the </body> tag.

    But you can load the script and delay the execution of the function(s) contained within it by

    window.onload = function() {
    functionOne();
    functionTwo();
    }

    As your script is loaded from an external source this may not be posssible.

    Quizmaster: How many centimeters are there in a metre?
    Contestant: Two and a half.
    Last edited by Philip M; 07-27-2010 at 09:11 PM.

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Try this. Anything else using onload must be combined with it.
    Code:
    <h2 id='myElem'>Welcome Tony Website</h2>
    
    ............
    
    <script type="text/javascript">
    
    window.onload = function()
    {
      var myScript = document.createElement( 'script' ), 
          elem = document.getElementById( 'myElem' );
    
      myScript.src = "http://localtimes.info/clock.php?cp3_Hex=0F0200&cp2_Hex=FFFFFF&cp1_Hex=000080&fwdt=118&ham=0&hbg=0&hfg=0&sid=&mon=&wek=&wkf=&sep=&continent=Asia&country=Indonesia&city=Jakarta&widget_number=116";
    
      elem.parentNode.insertBefore( myScript, elem.nextSibling );
    }
    </script>

  • #4
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question

    ok, how to put these codes in My page?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>My test page</title>
    </head>
    
    <body>
    <h1>Tony</h1>
    <h2>Welcome Tony Website</h2>
    <script type="text/javascript" src="http://localtimes.info/clock.php?cp3_Hex=0F0200&cp2_Hex=FFFFFF&cp1_Hex=000080&fwdt=118&ham=0&hbg=0&hfg=0&sid=&mon=&wek=&wkf=&sep=&continent=Asia&country=Indonesia&city=Jakarta&widget_number=116"></script>
    <h3>Tony is ... and is....</h3>
    <h3>He will....</h3>
    </body>
    
    </html>

  • #5
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by Tony M View Post
    ok, how to put these codes in My page?
    You can put the <script> block pretty much anywhere since it does not execute until the document has finished loading, at which time it will insert your specified script immediately after the element with id='myElem'.
    Presumably your script is suitable for insertion into a loaded document.

  • #6
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts
    The code that You showed Me above is not working, please help?
    Last edited by Tony M; 07-28-2010 at 07:59 PM.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Phil & Philip: I think you are misdiagnosing his problem.

    That script he is invoking is used to document.write code to embed a Flash object in the page.

    Just point your browser to that url:
    http://localtimes.info/clock.php?cp3...get_number=116
    to see the JS code.

    So he *CAN'T* delay it until page load. Period. He *MUST* create the Flash object inline.

    *IF* he had access to the code, he could change it so that the Flash movie doesn't start until requested, but that's about all he could do.

    He could also dump out what the code is generating and, instead of using that code just embed the Flash by hand and than change when the movie starts.

    But he can't do it by simply using onload to delay when the code is invoked.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by Old Pedant View Post
    Phil & Philip: I think you are misdiagnosing his problem.

    That script he is invoking is used to document.write code to embed a Flash object in the page.
    Well I did warn about that possibility, but now I know there's only one thing for it.

    1) Remove the code I gave you.

    2) Place your original <script> tags for the clock at the end of the <body> section, then immediately below those tags (still inside the body) add this script block:
    Code:
    <script type='text/javascript'>
    
    (function()
    {
     var objs = document.getElementsByTagName('object'), 
         me = document.getElementById( 'myElem' );  
       
     for( var i = 0, found = false, clsId; i < objs.length && !found; i++ )
     {
      clsId = objs[i].getAttribute('classid') || "";
          
      if( clsId === "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" )
      {
       found = true;  
       me.parentNode.insertBefore( objs[i], me.nextSibling );
      }
     } 
    })();
    
    </script>
    3) Make sure this element has the ID as shown here: <h2 id='myElem'>Welcome Tony Website</h2>

  • Users who have thanked RandomUser531 for this post:

    Tony M (07-29-2010)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Doesn't he also need to remove the objs[i] object?? Or does the insertBefore just move it, automatically, because it can't be in two places at once?

    And what's the purpose of the found flag?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by Old Pedant View Post
    Doesn't he also need to remove the objs[i] object?? Or does the insertBefore just move it, automatically, because it can't be in two places at once?
    Yes it's automatic, although Opera used to be a bit shaky about it.
    And what's the purpose of the found flag?
    To end the loop prematurely in an overt manner if further iterations are redundant.

  • #11
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question

    ok, finaly the script is working,
    BUT it is working fine only on Internet Explorer, but not on Firefox, on Firefox the clock is Hovering above the text that is supposed to be after it, and not under it? So what is the solution?
    Last edited by Tony M; 07-29-2010 at 02:52 PM.

  • #12
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by Tony M View Post
    ok, finaly the script is working,
    BUT it is working fine only on Internet Explorer, but not on Firefox, on Firefox the clock is Hovering above the text that is supposed to be after it, and not under it? So what is the solution?
    That will just be a page-layout issue. Try adding a <p> or <br> tag after your <h2> and transfer id='myElem' to that tag. Whatever you do, remember that the clock will be inserted immediately after whichever element has id='myElem'.

  • Users who have thanked RandomUser531 for this post:

    Tony M (07-29-2010)

  • #13
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question

    Ok, the script is working, thanks.
    I had those last 2 questions:

    1- If I put the srcipt (that You showed Me above)
    Code:
    <script type='text/javascript'>
    
    (function()
    {
     var objs = document.getElementsByTagName('object'), 
         me = document.getElementById( 'myElem' );  
       
     for( var i = 0, found = false, clsId; i < objs.length && !found; i++ )
     {
      clsId = objs[i].getAttribute('classid') || "";
          
      if( clsId === "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" )
      {
       found = true;  
       me.parentNode.insertBefore( objs[i], me.nextSibling );
      }
     } 
    })();
    
    </script>
    does it make all the JavaScript scripts in My page to load after the loading time?
    (for example I had a second clock but the script of the other clock isn't directly over the script that You showed Me, so does this other clock load after the page loads, or it will load normally with the page according to it's position)

    2- If I put a Javascript code that shows that the page is loading "Please Wait", does the clock code (that We discussed in the posts above), will be loaded before or after the loading message (please wait) disappears?
    Last edited by Tony M; 07-29-2010 at 04:53 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •