Error if I remove Alert ?!

Discussion in 'Javascript' started by sarah.ali1@gmail.com, Jul 31, 2006.

  1. Guest

    I had an alert statement in my code for debugging purposes. However if
    I try to remove the line of code with the alert statement in it (
    alert('message') ), I get an error in my code!

    Any ideas?
     
    , Jul 31, 2006
    #1
    1. Advertising

  2. Guest

    Here is the function with the code:

    function Start1(event) {

    include("phase.js"); // a function that includes an external javascript
    file on the fly
    include("answerkey.js");

    alert('debug message') //<---THIS is the problem, if i remove this
    line, I get an error

    getPhases(); //another function which works fine as long as the alert
    is there

    }

    Any help would be great
     
    , Jul 31, 2006
    #2
    1. Advertising

  3. RobG Guest

    wrote:
    > Here is the function with the code:
    >
    > function Start1(event) {
    >
    > include("phase.js"); // a function that includes an external javascript
    > file on the fly


    And that is the real code in question. How does include() actually
    include the script file? The best method is:

    function loadJSFile(fileURL){
    var newScript = document.createElement('script');
    newScript.type = "text/javascript";
    newScript.src = fileURL;
    document.getElementsByTagName('head')[0].appendChild(newScript);
    }

    <URL:
    http://groups.google.com/group/comp...=load external script&rnum=7#6f16ebc326f0eb9a
    >



    I'm not keen on using 'include' as the name of the function, it's not a
    reserved word but it may be misleading for those who come from another
    programming environment.


    --
    Rob
     
    RobG, Jul 31, 2006
    #3
  4. Randy Webb Guest

    said the following on 7/30/2006 8:39 PM:
    > Here is the function with the code:
    >
    > function Start1(event) {
    >
    > include("phase.js"); // a function that includes an external javascript
    > file on the fly
    > include("answerkey.js");
    >
    > alert('debug message') //<---THIS is the problem, if i remove this
    > line, I get an error
    >
    > getPhases(); //another function which works fine as long as the alert
    > is there
    >
    > }
    >
    > Any help would be great
    >


    It is a timing issue where you are trying to call a function in the
    external file before that file gets loaded.

    The browser won't wait for that file to load before continuing. The
    alert pauses the script execution long enough for the file to load and
    then you get no errors. Removing the alert removes that pause so you get
    the error.

    It is not the alert that is stopping the error but rather it is the stop
    in execution caused by the alert that prevents the error.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Jul 31, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,380
    Henrik
    Aug 10, 2005
  2. tshad
    Replies:
    6
    Views:
    21,475
    tshad
    Aug 8, 2006
  3. Mersh
    Replies:
    0
    Views:
    498
    Mersh
    Mar 13, 2007
  4. Ganesh
    Replies:
    0
    Views:
    562
    Ganesh
    Jun 29, 2007
  5. Replies:
    8
    Views:
    433
    Mark Rae [MVP]
    Oct 23, 2007
Loading...

Share This Page