Arg! Hours spent trying to figure this out: dynamicly loaded javascript functions!

Discussion in 'JavaScript' started by stompers, Jul 17, 2007.

  1. #1
    Hello there,

    I'm trying to find a practical example of something that uses this pattern, which dynamically loads javascript into a page that has already been loaded:

    http://72.14.253.104/search?q=cache:3K5QT816OGsJ:ajaxpatterns.org/On-Demand_Javascript

    I have found numerous examples of this, however what I'm really looking for is an example that shows how to dynamically load a javascript file with functions, and then check to see if the page or function is loaded, and then execute the function. If you just execute the function after loading the javascript, it won't be fully loaded yet and the function won't fire.

    I've been searching through the web for several hours trying to find a solution, but have not found a good one yet. Any help would be much appreciated!!!

    Thank you,
    Chris
     
    stompers, Jul 17, 2007 IP
  2. stompers

    stompers Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello Mike,

    Thank you for your response. I've seen that example a a couple of times, but if you do something like this:

    <input type="button" value="Insert new.js file" onclick="init();somethingNew();">
    HTML:
    Which grabs the js file, and the executes the function, the function won't run because the client hasn't gotten the js file yet. From what I read, it's a problem with the asynchronous nature of javascript. However, in the article I mention below, it mentions off-handedly that there are some potential work-arounds, but I haven't been able to dig up any examples. Do you or anyone else have any further suggestions?

    Thank you!
    Chris
     
    stompers, Jul 17, 2007 IP
  3. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <input type="button" value="Call new function" onclick="init();setTimeout('somethingNew()',25)">
    Code (markup):
     
    Mike H., Jul 17, 2007 IP
  4. stompers

    stompers Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Mike,

    Thank you for the idea, but the timeout would be a last resort solution since: 1) you don't know how long it will take to fetch the file (i.e. what if the server is busy?), and 2) you're wasting a user's time during the timeout. If you use this dynamic javascript method often, the timeouts could really add up and have a high cost to the user interface.

    Do you have any other ideas? Anyone?

    Thanks!
    Chris
     
    stompers, Jul 17, 2007 IP