Reputation: 1922
I must include jQuery in a page using code:
if (typeof window.jQuery === "undefined") {
document.write('<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"><\/script>');
if (typeof Prototype !== "undefined") {
document.write('<script>jQuery.noConflict();<\/script>');
}
}
But then if I try to use jQuery, Firebug logs "jQuery undefined".
jQuery(document).ready(function() {
//
});
but jquery is correctly loaded
Upvotes: 1
Views: 185
Reputation: 3186
The way the HTML5 boilerplate does it is the most efficient way.
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.0.min.js"><\/script>')</script>
This will check to see if jQuery has been defined yet (I.e loaded), or (if not) it will load write the script to the page :).
Upvotes: 1
Reputation: 97672
I would suggest against using document.write, use dom manipulation.
if (typeof window.jQuery === "undefined") {
var script = document.createElement('script');
script.onload = function(){
if (typeof Prototype !== "undefined") {
jQuery.noConflict();
}
}
script.src = 'http://code.jquery.com/jquery-latest.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
}
Upvotes: 3
Reputation: 16923
window.onload = function() {
jQuery(document).ready(function() {
alert('!');
});
}
do the trick.
Upvotes: 1