Jitender
Jitender

Reputation: 7969

load function when everything is loaded

I have jquery function that need to implement in .aspx file, in that file divs are generating on run time so my requirement is when all the structure is loaded then my function should load.

Upvotes: 1

Views: 111

Answers (3)

gotqn
gotqn

Reputation: 43666

Actually, there are a lot of situations when

$(document).ready(function() {
// actions to perform
};

will not work as desire. For example, when you have content that is generated by JavaScript after the page is loaded (because it takes a lot of time or it is waiting for information from server).

So, what I usually use is combination of these:

$(document).ready(function() {

    (function IsElementLoaded(){
        //you can check for desire element(s) changing the jquery selector
        if($('#MyElementID').length==0){
            setTimeout(IsElementLoaded(),100);
        }else{
            //your code
        }
    }());
});

Upvotes: 1

KyorCode
KyorCode

Reputation: 1497

Perform a <script /> action at the end of your page or use

$(document).ready(function() {
// actions to perform
};

Upvotes: 1

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91349

Either put your code in the .ready() handler:

$(document).ready(function () {
  // your code
});

Which is also equivalent to:

$(function() {
  // your code
});

Or execute your function at the end of the document.

Upvotes: 3

Related Questions