SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What scope to put function in?

    Hi All,

    I have a .js file which uses AJAX to query some fields from a DB, and then document.writes a few div's using those fields. I then need to start an interval that calls importantFunction() every 4 seconds. The problem is, I dont know where to place importantFunction(). I have tried the POSSIBLE POSITIONS mentioned in the code below, but all give a "importantFunction not declared" sort of javascript error.

    And help would be appreciated.

    Code:
    function ajaxFunction()
    {
      var ajaxRequest;
    
      try
      {
        ajaxRequest = new XMLHttpRequest();
      }
      catch (e)
      {
        try
        {
          ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
          try
          {
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch (e)
          {
            alert("Your browser broke!");
            return false;
          }
        }
      }
    
      ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4)
        {
          returnVal = ajaxRequest.responseText;
    
          for(fd=1; fd<=total; fd++)
          {
            document.write('<div>');
            document.write(fd + ": " + returnVal);
            document.write('</div>');
    
            //POSSIBLE POSITION 2, USING document.write
            //POSSIBLE POSITION 3, JUST AS A JAVASCRIPT FUNCTION
          }
          document.close();
    
          var t = setInterval('importantFunction()',4000);
        }
      }
    
      ajaxRequest.open("GET", "queryDB.php", true);
      ajaxRequest.send(null); 
    }
    
    //POSSIBLE POSITION 1
    
    ajaxFunction();

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A few things to note:

    1) Possible position 1 is the best place
    2) The reason it's coming up as undeclared despite the positioning is most likely a javascript error someplace else. If the code has a fatal error anywhere, the javascript parser will simply stop there, causing things to fail.
    3) document.write is a bad habit, use the DOM functions like:

    Code:
    var div = document.createElement('div');
    div.innerHTML = fd + ": " + returnVal;
    document.body.appendChild(div);
    If you're doing debug stuff, alert might be easier to use instead. But yeah, check your browser's javascript debug console to find out if something is erroring out.

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure document.write() will start a new document, overwriting the previous one, if called once the page has loaded. In that case, your importantFunction will no longer exist.

    Try using DOM methods like document.createElement('div'), document.createTextNode() and appendChild() rather than document.write()

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Indeed modifying the DOM is a better way to do this. Manipulating the DOM and placing the function in Possible position1 fix the issue

    Thanks a lot for the help!

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    document.write can only be used before the page finishes loading and so the first one after the page finishes loading is the cause of the problem as what it writes out will replace the entire page.

    If using the proper DOM commands to insert into the page is too difficult for you to work out you can always use innerHTML. At least that works after the page has loaded.

    Of course all of these options assume that you are using HTML and not XHTML since document.write(), innerHTML, and createElement() etc only work for HTML. For XHTML the only option to use is document.createElementNS() since all elements added to an XHTML document must specify both what tag you are adding and the namespace you are adding it to.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •