www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] How to execute function after div loads?

  1. #1
    Join Date
    Jul 2009
    Posts
    27

    resolved [RESOLVED] How to execute function after div loads?

    How to execute function after div loads


    heres my function, if div exists return "true", now how to use chkObject('div') do loop until return true?
    Code:
    function chkObject (theVal)
    {
       if (document.getElementById(theVal) != null)
       {
          return true;
       }
       else
       {
          return false;
       }
    }
    I found jquery code but does not work!
    Code:
    $("#div1").load(function() {
      alert("The div has been loaded.");
    });
    div
    HTML Code:
    <html>
    
    <head>
    <title></title>
    <style>
    #div1 {background-color : fffccc;width : 100;height : 100;}
    </style>
    </head>
    
    <body>
    
    <div id="div1">
      <p>my div1</p>
    </div>
    
    </body>
    
    </html>
    any help would be appreciated
    best regards

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    One way to do it is to put the call to

    Code:
     
    function chkObject(elemId) {
    	return (document.getElementById(elemId))? true : false;
    }
    in a setInterval() and then put the setInterval() in window.onload to start it automatically when the page loads.

    When chkObject(elemId) returns true, clear the setInterval() and then execute whatever javascript you need.
    Last edited by tirna; 08-20-2010 at 08:37 PM.

  3. #3
    Join Date
    Jul 2009
    Posts
    27
    hi tirna thanks a lot for your reply, i don't want to use window.onload is there any other way!

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    Another option is to put the setInterval() in a <script> block within the <body> or <head>

    btw - just out of curiosity is the reason you don't want to use window.onload because this is for some kind of school project and you haven't covered window.onload yet?
    Last edited by tirna; 08-20-2010 at 09:06 PM.

  5. #5
    Join Date
    Jul 2009
    Posts
    27
    i don't want to use window.onload, Just because i don't want to wait for the entire document to load

  6. #6
    Join Date
    Mar 2010
    Posts
    2,803
    ok, then all you have to do is put the setInterval() in a <script> block wherever you would like it to start in the <head> or <body>

  7. #7
    Join Date
    Jul 2009
    Posts
    27
    @tirna Thank you very much. i really appreciate your help, its working

    Code:
    var int=setInterval('check()', 500);
    function check()
    {
       if (chkObject('div')==true)
       {
          window.alert('true');
          int=window.clearInterval(int)
       }
       else
       {
          document.write('<p>false </p>')
       }
    }
    
    function chkObject(elemId)
    {
       return (document.getElementById(elemId))? true : false;
    }

  8. #8
    Join Date
    Mar 2010
    Posts
    2,803
    no problem - happy to help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles