check if jquery has been loaded, then load it if false
Maybe something like this:
<script>
if(!window.jQuery)
{
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "path/to/jQuery";
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
Checking if jquery is loaded using Javascript
something is not right
Well, you are using jQuery to check for the presence of jQuery. If jQuery isn't loaded then $()
won't even run at all and your callback won't execute, unless you're using another library and that library happens to share the same $()
syntax.
Remove your $(document).ready()
(use something like window.onload
instead):
window.onload = function() {
if (window.jQuery) {
// jQuery is loaded
alert("Yeah!");
} else {
// jQuery is not loaded
alert("Doesn't Work");
}
}
Detect if page has finished loading
jQuery(window).load(function () {
alert('page is loaded');
setTimeout(function () {
alert('page is loaded and 1 minute has passed');
}, 60000);
});
Or http://jsfiddle.net/tangibleJ/fLLrs/1/
See also http://api.jquery.com/load-event/ for an explanation on the jQuery(window).load.
Update
A detailed explanation on how javascript loading works and the two events DOMContentLoaded and OnLoad can be found on this page.
DOMContentLoaded: When the DOM is ready to be manipulated. jQuery's way of capturing this event is with jQuery(document).ready(function () {});
.
OnLoad: When the DOM is ready and all assets - this includes images, iframe, fonts, etc - have been loaded and the spinning wheel / hour class disappear. jQuery's way of capturing this event is the above mentioned jQuery(window).load
.
Detect Jquery with regular javascript, if not present load it dynamcally
Something like this should work.
EDIT: Code added from above link.
var jQueryScriptOutputted = false;
function initJQuery() {
//if the jQuery object isn't available
if (typeof(jQuery) == 'undefined') {
if (! jQueryScriptOutputted) {
//only output the script once..
jQueryScriptOutputted = true;
//output the script (load it from google api)
document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"></scr" + "ipt>");
}
setTimeout("initJQuery()", 50);
} else {
$(function() {
//do anything that needs to be done on document.ready
});
}
}
initJQuery();
How to determine if jQuery is running on a page (even if jQuery is /after/ the detection script)
Given your constraints, I see only two options:
Use
window.load
event:(function() {
if (window.addEventListener) {
// Standard
window.addEventListener('load', jQueryCheck, false);
}
else if (window.attachEvent) {
// Microsoft
window.attachEvent('onload', jQueryCheck);
}
function jQueryCheck() {
if (typeof jQuery === "undefined") {
// No one's loaded it; either load it or do without
}
}
})();window.load
happens very late in the loading cycle, though, after all images are and such loaded.Use a timeout. The good news is that the timeout can probably be quite short.
(function() {
var counter = 0;
doDetect();
function doDetect() {
if (typeof jQuery !== "undefined") {
// ...jQuery has been loaded
}
else if (++counter < 5) { // 5 or whatever
setTimeout(doDetect, 10);
}
else {
// Time out (and either load it or don't)
}
}
})();You'll have to tune to decide the best values for the counter and the interval. But if jQuery isn't loaded even on the first or second loop, my guess (untested) is that it isn't going to be loaded...unless someone else is doing what you're doing. :-)
How can I load jQuery if it is not already loaded?
jQuery is not available immediately as you are loading it asynchronously (by appending it to the <head>
). You would have to add an onload listener to the script (jqTag
) to detect when it loads and then run your code.
e.g.
function myJQueryCode() {
//Do stuff with jQuery
}
if(typeof jQuery=='undefined') {
var headTag = document.getElementsByTagName("head")[0];
var jqTag = document.createElement('script');
jqTag.type = 'text/javascript';
jqTag.src = 'jquery.js';
jqTag.onload = myJQueryCode;
headTag.appendChild(jqTag);
} else {
myJQueryCode();
}
Related Topics
Convert Js Object to Form Data
Npm Install Cannot Find Module 'Semver'
A Proper Wrapper for Console.Log with Correct Line Number
Jquery - Get Text for Element Without Children Text
How to Iterate (Keys, Values) in JavaScript
Jquery Equivalent of JavaScript's Addeventlistener Method
How to Send an Ajax Request on a Different Port with Jquery
Browser Event When Downloaded File Is Saved to Disk
Ignore Typescript Errors "Property Does Not Exist on Value of Type"
Nodemailer with Gmail and Nodejs
Correct Way to Handle Conditional Styling in React
Break Promise Chain and Call a Function Based on the Step in the Chain Where It Is Broken (Rejected)
How to Return a JavaScript String from a Webassembly Function
Is Reading the 'Length' Property of an Array Really That Expensive an Operation in JavaScript
How to Perform an Http File Upload Using Express on Cloud Functions for Firebase (Multer, Busboy)