Preloading a script?

Discussion in 'JavaScript' started by Kestal, Oct 17, 2008.

  1. #1
    I am new to Java and trying to learn pretty quickly. I have tried to create my own simple java text time script. Works just as well as other java time codes with only about 10-15 lines. I am fine with it.

    Works great just by itself.

    I plug it into my site and it looks like it works... When I refresh, the majority of the time its perfect. Then, in other cases, the script for google ads slows it down and it stays blank for about a second THEN pops up. Is there any way to preload a specific script before anything else in an efficient way? or even to make sure the script loads up prior to loading any other script?

    Is this common or have I done something horribly wrong? There will not be much more java anywhere else on my website.
     
    Kestal, Oct 17, 2008 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    It depends on how the script works and where everything is located in your page.

    If the place where the timer gets displayed is before the Adsense banner, you should be able to place the script either before or after that place depending on how your script works.

    If the timer is displayed after an Adsense banner, it will likely take some tweeking of your layout to get your timers display HTML to show up before your banner HTML while keeping the visual aspects the same.

    Adsense code uses "document.write" which makes it tough to move the code around & impossible to use the "defer" attribute with because it will cause the browser to clear the page before writing.
     
    joebert, Oct 18, 2008 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    check domready. in particular, move adsense to a domready / window.load / onload (whatever you or the frameworks want to call it). This will solve it.

    you get the delay because the browser does not know if the script it's trying to load may modify the dom rendered thus far so it stops and waits. the kind of modification like document.write etc - really bad practice.

    the idea is, scripts only get to run once the whole page is rendered and the dom is complete. it makes great sense to do that and the benefits are not just speed of loading...
    more on the concept here:
    http://walkthrough.ifupdown.com/walkthrough-1.2/domready

    learn to always code this way now and you will LOVE it in the future - plus it will save you hours of refactoring your old work

    just a tip: java != javascript. totally - not - the - same. this is the javascript forum, just to double check?
     
    dimitar christoff, Oct 18, 2008 IP