Skip to content

Commit

Permalink
Much better handling for jQuery loading
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhartha-gupta committed Jul 20, 2014
1 parent d7ca22e commit d07b524
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
84 changes: 71 additions & 13 deletions Sources/LikePosts.php
Expand Up @@ -46,11 +46,46 @@ function LP_includeAssets() {
inConflict = false;
// Only do anything if jQuery isn"t defined
if (typeof jQuery == "undefined") {
if (typeof(jQuery) == "undefined") {
console.log("jquery not found");
if (typeof($) == "function") {
console.log("jquery but in conflict");
inConflict = true;
}
function compareJQueryVersion(v1, v2, callback) {
var v1parts = v1.split('.');
var v2parts = v2.split('.');
for (var i = 0; i < v1parts.length; ++i) {
if (v2parts.length == i) {
//v1 + " is larger"
callback(1);
return;
}
if (v1parts[i] == v2parts[i]) {
continue;
} else if (v1parts[i] > v2parts[i]) {
//v1 + " is larger";
callback(1);
return;
} else {
//v2 + " is larger";
callback(2);
return;
}
}
if (v1parts.length != v2parts.length) {
//v2 + " is larger";
callback(2);
return;
}
callback(false);
return;
}
function loadJquery(url, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
Expand All @@ -62,8 +97,6 @@ function loadJquery(url, callback) {
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
// callback function provided as param
callback();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
Expand All @@ -73,20 +106,45 @@ function loadJquery(url, callback) {
};
loadJquery("http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js", function() {
if (typeof jQuery=="undefined") {
console.log("still not loaded");
} else {
if (inConflict === false) {
loadLPScript();
} else {
// Use no conflict over here
loadLPScript();
}
if (typeof(jQuery) !=="undefined") {
console.log("directly loaded with version: " + jQuery.fn.jquery);
lp_jquery2_0_3 = jQuery.noConflict(true);
loadLPScript();
}
});
} else {
// jQuery is already loaded
loadLPScript();
console.log("jquery is already loaded with version: " + jQuery.fn.jquery);
compareJQueryVersion(jQuery.fn.jquery, "2.0.3", function(result) {
console.log("result of version check: " + result)
switch(result) {
case false:
case 1:
lp_jquery2_0_3 = jQuery.noConflict(true);
loadLPScript();
break;
case 2:
loadJquery("http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js", function() {
if (typeof(jQuery) !=="undefined") {
console.log("after version check loaded with version: " + jQuery.fn.jquery);
lp_jquery2_0_3 = jQuery.noConflict(true);
loadLPScript();
}
});
break;
default:
loadJquery("http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js", function() {
if (typeof(jQuery) !=="undefined") {
console.log("default version check loaded with version: " + jQuery.fn.jquery);
lp_jquery2_0_3 = jQuery.noConflict(true);
loadLPScript();
}
});
break;
}
})
};
function loadLPScript() {
Expand Down
2 changes: 1 addition & 1 deletion Themes/default/scripts/LikePostStats.js
Expand Up @@ -263,7 +263,7 @@

this.likePostStats = likePostStats;
if (typeof(likePostStats.jQRef) !== "function" && typeof(likePostStats.jQRef) === "undefined") {
likePostStats.jQRef = jQuery.noConflict();
likePostStats.jQRef = lp_jquery2_0_3;
}

likePostStats.jQRef(".like_post_stats_menu a").on("click", function(e) {
Expand Down
2 changes: 1 addition & 1 deletion Themes/default/scripts/LikePosts.js
Expand Up @@ -469,7 +469,7 @@ likePosts.prototype.removeOverlay = function(e) {

var lpObj = window.lpObj = new likePosts();
if (typeof(lpObj.jQRef) !== 'function' && typeof(lpObj.jQRef) === 'undefined') {
lpObj.jQRef = jQuery.noConflict();
lpObj.jQRef = lp_jquery2_0_3;
}

(function() {
Expand Down

0 comments on commit d07b524

Please sign in to comment.