Lazy jQuery: How to load jQuery dyamically
Ranked #968 in Education, #22,577 overall
jQuery on the fly.
This lens is intended for advanced jQuery users with a rather specific need. If this isn't you, reading the lens is probably a waste of your time. The solution described in this article dynamically loads the jQuery javascript library without using ajax.
I came across the need to load jQuery based on an event after a basic html page had already loaded. This had to be done synchronously with any other code which depends on jQuery. I couldn't find a solution online that suited my needs, so thought I'd share. It's pretty simple to figure out but may save you some time.
Contents at a Glance
Introduction
Why Would I Want to Load jQuery After Site my Finishes Loading?
The basic idea:
- Call a function that loads jQuery.
- Continuously poll until jQuery has loaded.
- Run some code that requires jQuery to be ready.
Step 1:
<html>
<head>
</head>
<body>
<div id='status'>jQuery is not loaded yet.</div>
<input type='button' value='Click here to load it.' onclick='load()' />
</body>
</html>
Step 2:
<script>
load = function() {
load.getScript("jquery-1.2.6.js");
load.tryReady(0); // We will write this function later. It's responsible for waiting until jQuery loads before using it.
}
// dynamically load any javascript file.
load.getScript = function(filename) {
var script = document.createElement('script')
script.setAttribute("type","text/javascript")
script.setAttribute("src", filename)
if (typeof script!="undefined")
document.getElementsByTagName("head")[0].appendChild(script)
}
</script>
Step 3:
If jQuery isn't found after 5 seconds of trying, this function times out and stops trying.
load.tryReady = function(time_elapsed) {
// Continually polls to see if jQuery is loaded.
if (typeof $ == "undefined") { // if jQuery isn't loaded yet...
if (time_elapsed <= 5000) { // and we havn't given up trying...
setTimeout("load.tryReady(" + (time_elapsed + 200) + ")", 200); // set a timer to check again in 200 ms.
} else {
alert("Timed out while loading jQuery.")
}
} else {
// Any code to run after jQuery loads goes here!
// for example:
$("#status").css({
'font-size':'20px',
'color':'green'}).text("jQuery is loaded and ready to go!");
}
}
Now your page will load faster than ever since it only loads jQuery when needed!
Click here to open the whole demo in one page.
Questions or Comments? Let Me Know!
-
Reply
-
blogfreakz-
Feb 7, 2012 @ 1:19 am | delete
- really interesting and informative lens I love jquery
jQuery tutorials for beginners
-
-
Reply
-
nbernhard
Oct 18, 2011 @ 11:46 am | delete
- Very nice indeed. How much of a performance hit does jQuery add to the page? I mean, we've got to be talking about milliseconds. Just wondering how worth it this is to implement.
-
-
Reply
-
reflyh2
Aug 30, 2011 @ 12:53 pm | delete
- Interesting trick. Not sure whether I will need it for jQuery, but still nice to know, and can be used to load other JavaScript files.
-
-
Reply
-
qihang
Jul 26, 2011 @ 8:25 pm | delete
- http://fruitkoo.blogbus.com/logs/151119035.html
http://fruitkoo.blogbus.com/logs/151123969.html
http://fruitkoo.blogbus.com/logs/151129386.html
http://fruitkoo.blogbus.com/logs/151131819.html
http://fruitkoo.blogbus.com/logs/151133724.html
http://blog.cnfol.com/fruitkoo/article/50436494.html
http://blog.cnfol.com/fruitkoo/article/50437799.html
http://blog.cnfol.com/fruitkoo/article/50438767.html
http://blog.cnfol.com/fruitkoo/article/50439048.html
http://blog.cnfol.com/fruitkoo/article/50439189.html
http://fruitkoo.blog.163.com/blog/static/191036265201162695126747/
http://fruitkoo.blog.163.com/blog/static/191036265201162695759326/
http://fruitkoo.blog.163.com/blog/static/191036265201162610412908/
http://fruitkoo.blog.163.com/blog/static/191036265201162610956701/
http://fruitkoo.blog.163.com/blog/static/1910362652011626101456911/
http://blog.tianya.cn/blogger/post_read.asp?BlogID=3790948&PostID=34250706
http://blog.tianya.cn/blogger/post_read.asp?BlogID=3790948&PostID=34250956
http://blog.tianya.cn/blogger/post_read.asp?BlogID=3790948&PostID=34251240
-
-
Reply
-
aleskotnik
Jul 18, 2011 @ 11:09 am | delete
- Great post! You might be interested about ajax and search engines. Check my lenses :-)
-
- Load More
by echo85
A programmer who's learning about javascript, jQuery and lenses today.
- 1 featured lens
- Winner of 2 trophies!
- Top lens » Lazy jQuery: How to load jQuery dyamically