26

I have the following in my javascript file:

var divId = "divIDer";

jQuery(divId).ready(function() {
  createGrid();  //Adds a grid to the html
});

The html looks something like:

<div id="divIDer"><div>

But sometimes my createGrid() function gets called before my divIder is actually loaded onto the page. So then when I try to render my grid it can't find the proper div to point to and doesn't ever render. How can I call a function after my div is completely ready to be used?

Edit:

I'm loading in the div using Extjs:

var tpl = new Ext.XTemplate(
'<div id="{divId}"></div>');

tpl.apply({

});
Grammin
  • 11,808
  • 22
  • 80
  • 138

5 Answers5

40

You can use recursion here to do this. For example:

jQuery(document).ready(checkContainer);

function checkContainer () {
  if ($('#divIDer').is(':visible')) { //if the container is visible on the page
    createGrid(); //Adds a grid to the html
  } else {
    setTimeout(checkContainer, 50); //wait 50 ms, then try again
  }
}

Basically, this function will check to make sure that the element exists and is visible. If it is, it will run your createGrid() function. If not, it will wait 50ms and try again.

Note:: Ideally, you would just use the callback function of your AJAX call to know when the container was appended, but this is a brute force, standalone approach. :)

Matthew Blancarte
  • 8,251
  • 2
  • 25
  • 34
  • I am loading five pages on iframe on by one, one of them the page is the google map geocode, i am calling a function on window onload to geocode the address but that funtion fire when first page load in the iframe, i tried the document.ready,wind.load,if($(div).is(:visible)) but every thing fires on first page load on the iframe, the googlemap is on number three when we click next 3 times then googlemap turn come!! – – Abdul Muheet Oct 10 '15 at 07:48
  • Hey @Mohit, I think it would be best for you to really lay out your code in a new question. Too hard to follow in comment context. If you link your new question here, I will follow it and see if I can help. :) – Matthew Blancarte Oct 10 '15 at 19:07
8

Thus far, the only way to "listen" on DOM events, like inserting or modifying Elements, was to use the such called Mutation Events. For instance

document.body.addEventListener('DOMNodeInserted', function( event ) {
    console.log('whoot! a new Element was inserted, see my event object for details!');
}, false);

Further reading on that: MDN

The Problem with Mutation Events was (is) they never really made their way into any official spec because of inconcistencies and stuff. After a while, this events were implemented in all modern browser, but they were declared as deprecated, in other words you don't want to use them.


The official replacement for the Mutation Events is the MutationObserver() object.

Further reading on that: MDN

The syntax at present looks like

var observer = new MutationObserver(function( mutations ) {
    mutations.forEach(function( mutation ) {
         console.log( mutation.type );
    }); 
});

var config = { childList: true };

observer.observe( document.body, config );

At this time, the API has been implemented in newer Firefox, Chrome and Safari versions. I'm not sure about IE and Opera. So the tradeoff here is definitely that you can only target for topnotch browsers.

jAndy
  • 231,737
  • 57
  • 305
  • 359
4

To do something after certain div load from function .load(). I think this exactly what you need:

  $('#divIDer').load(document.URL +  ' #divIDer',function() {

       // call here what you want .....


       //example
       $('#mydata').show();
  });
Mohamad Osama
  • 858
  • 9
  • 10
1

Through jQuery.ready function you can specify function that's executed when DOM is loaded. Whole DOM, not any div you want.

So, you should use ready in a bit different way

$.ready(function() {
    createGrid();
});

This is in case when you dont use AJAX to load your div

Pavel Gruba
  • 1,999
  • 1
  • 12
  • 8
1

inside your <div></div> element you can call the $(document).ready(function(){}); execute a command, something like

<div id="div1">
    <script>
        $(document).ready(function(){
         //do something
        });
    </script>
</div>

and you can do the same to other divs that you have. this was suitable if you loading your div via partial view

dr.Crow
  • 1,493
  • 14
  • 17
  • That's a righteous hack. I think you voided the warranty. I'm using this. – J E Carter II Feb 09 '18 at 13:13
  • As far as I know this would load the DOM (and load the script with it) but then still only executes when the full page is ready because you put it in `$(document).ready()`. Remove that and it'll work, but I'm assuming this is not a solution Grammin can use. – s1h4d0w Jan 10 '19 at 12:17