So, I'm loading some data from a MVC3 action that returns Json, containing some parameters and content as a string. I append the content to some div. In the partial view I have a document.ready JQuery event. For some reason that function executes before the content is appended and all the selectors I declare in the ready function are empty.
Is there a logic reason for this? Is I set a timeout the selectors see the elements. But a timeout can be very imprecise.
Any suggestions?
Thanks!
Example code fiddle: http://jsfiddle.net/aKxy7/
ready() function will load as soon as the DOM is loaded and before the page contents are loaded. You should wrap all your javascript code with this function to ensure that the code only runs when the page is fully rendered.
ready( handler )Returns: jQuery. Description: Specify a function to execute when the DOM is fully loaded.
The Document Ready Event This is to prevent any jQuery code from running before the document is finished loading (is ready). It is good practice to wait for the document to be fully loaded and ready before working with it.
$( document ). ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.
It sounds like you are expecting $(document).ready()
to fire after all assests are loaded. That's not how $(document).ready()
works. It is triggered when the DOM is finished rendering. Nothing more. It sounds like you want to use $(window).load()
, which does wait until all assets are loaded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With