Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Determine if script is loaded

  1. #1
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Determine if script is loaded

    I'm trying to add/load a javascript on the document dynamically. This part is okay, but I need to know if the script has been loaded completely or not so that from a 2nd script I can call functions that are on the first script(loading dynamically).

    What I have found so far by googling about it, looks like there's no way of doing it. I thought I would ask here in case I missed it. Without modifying the first script, which will be loaded dynamically, is there any way of finding if the script has been completely loaded or not? Does DOM have any properties for this?

    Let me know if you have any suggestions. I hope I explained it properly, if not, let me know and I'll try to elaborate it. Thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If the first script has any functions and/or variables in it that you are depending upon, just check to see if they are what they need to be before using them.

    Say the script has a function:

    Code:
    function reallyImporatant(){
    return 'magic things';
    }
    Before trying to use it, you could always test:

    Code:
    if(typeof reallyImporatant=='function')
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much for your reply jscheuer1. I'll try your suggestions, but I think I'm gonna have to use a for loop or a while loop when checking if it's a function. That might be another problem. From previous experience, I think this will put the browser into an infinite loop and then crash. Anyways, I'll try it out.

    Say the script has a function:

    Code:

    function reallyImporatant(){
    return 'magic things';
    }

    Before trying to use it, you could always test:

    Code:

    if(typeof reallyImporatant=='function')
    Now, let's say the function reallyImportant() is calling another function in the same script like this:

    Code:

    function reallyImporatant(){
    var val = getValue() ;
    }

    If the script didn't load completely but
    HTML Code:
    if(typeof reallyImporatant=='function')
    returns true and I call the function reallyImportant(), it's probably not going to work all the time. Again, I don't know if I was able to explain the problem properly.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Have the script call a callback when it's loaded?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    Have the script call a callback when it's loaded?
    Thanks Twey, But that would require me to modify the script. Also, how would I know when it's loaded? Correct me if I misunderstood your post.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Scripts generally load only one way, or consistently for certain browsers according to what methods they support and how that causes the script to branch. So, all you need to do is to determine what is the last thing to get assigned that's essential to the script's use. A well written script, even if it branches will end up with certain things defined, or at least one out of three, something like that. I'm not going to second guess what that is/these are in a script I haven't seen. If all else fails, it could be determined via trial and error.

    Now, I didn't mean to say that what I was offering was a complete solution. I had no way of knowing what you were going to do with that test. One use could be:

    Code:
    function myDependantFunc(){
    if(typeof reallyImporatant=='function'){
    do dependant stuff here;
    }
    else
    setTimeout("myDependantFunc()",300);
    }
    You could even count the loops and give up and do something else if - say a minute, or 3 seconds, or whatever passed and what you need from the other script wasn't ready yet.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks John and Twey. I think both you have a good solution. As Twey suggested, I could write a call back function in my script and someone else could use that if they are using my script. And if I'm using someone else's script, I guess I could use John's suggestion.

    I think now that web 2.0 is really spreading out and api is available from all over the place, may be we should look into dynamically loading javascript. Or else the page will take much longer to load than it should. Also, it probably doesn't make sense to load a whole bunch of javascript files at the beginning and most of it may not be used depending on what the user is doing on the page.

    That's why I think loading javascripts (same applies for CSS) dynamically is the best approach and also script functionalities should be separated in different files instead of having everything in the same file, but then I got stuck at the question that I asked in this thread.

    Thanks again.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think the up and coming solution to script loading is third party hosting of popular code libraries. That way, chances are that the user already has most of the code cached.

    With today's high speed connections though, you would need to have many, many very complex and long scripts to take much of a loading hit from just them. Images and things like audio, video, and Flash are, and will continue to be the real bandwidth hogs.

    Images can be optimized to speed page loading.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    But even a single image is usually many thousand times the size of a script. I don't think I've ever seen a 100k+ Javascript file.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    But? Don't you mean 'And'?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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