Getting jQuery to Work When Loaded Dynamically - J Query

Problem
You are including jQuery dynamically into the page, by either adding a <script>element to the DOM or doing it some other way like Ajax.Once jQuery is loaded, you expect everything to start working, but for some reason,no script starts.

Solution

You need to include an additional script to be executed after jQuery is loaded. This script will simply call jQuery.ready(). After you do this, everything will start working as expected.

Discussion

What is jQuery.ready()?

The jQuery.ready() function is called by jQuery’s core when the document is detected as ready. Once called, all the document.ready handlers are triggered automatically.

Why was this happening?

The document.ready detection is mostly based on events. Depending on the browser, acertain event is bound, and it’s supposed to be triggered once the document is ready.
In addition, the window.onload event is bound for all browsers as a fallback measure incase the other options fail.
What was happening to you is that jQuery was finally loaded into the page only after the window.onload event; therefore, all the event handlers remained bound, and none got triggered.
By calling jQuery. ready(), you’re “announcing” the document. ready event manually, triggering all the handlers and getting things back to normal.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics