I created a JS widget library for distribution purpose on external websites and would like your thoughts on it.
What widget does: It adds an always visible text on the screen. When user clicks on text, an external page gets loaded in iFrame in a lightbox. Internally, widgets loads jQuery library and some external JS and CSS files.
What I need to be reviewed:
- Check if prototype being used the right way and work across all browsers?
- If jQuery is correctly loaded and resolved correctly (even if the page hosting the script have same/different version of jQuery already running on the page)
- I used 'window.addEventListener' method to check if jQuery is loaded or not. Is it correct and the way ideally it should be?
- Is there any efficient way of loading the colorbox.js file
- Can the code be optimised further for improved performance?
- Is the code production ready?
HTML Code
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<a href="http://yahoo.com">Anchor - by user code</a>
<div style="height:1500px;"></div>
<a href="http://yahoo.com">Another Anchor - by user code</a>
<!-- Widget Script: Starts -->
<script type="text/javascript" src="widget.js" ></script>
<Script type="text/javascript">
var customWidget = new MyWidget();
customWidget.Render({ anchorText:'wikipedia', link: 'http://www.wikipedia.com/' });
</script>
<!-- Widget Script: Ends -->
</body>
</html>
JS Code - widget.js
var MyWidget = function () {
// Loading CSS in Async
var fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", 'widget.css');
document.getElementsByTagName("head")[0].appendChild(fileref);
fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", 'http://www.jacklmoore.com/colorbox/example4/colorbox.css');
document.getElementsByTagName("head")[0].appendChild(fileref);
// Localize jQuery variable
var jQuery;
// Load jQuery if not present
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.10.2') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
this.ResolveJqueryAndLoadAdditionalJsAndCss();
}
};
} else {
script_tag.onload = this.ResolveJqueryAndLoadAdditionalJsAndCss;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
this.ResolveJqueryAndLoadAdditionalJsAndCss();
}
};
MyWidget.prototype.ResolveJqueryAndLoadAdditionalJsAndCss = function () {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
};
MyWidget.prototype.Render = function (data) {
window.addEventListener("load", function () {
jQuery(document).ready(function ($) {
$.when(
$.getScript("http://www.jacklmoore.com/colorbox/jquery.colorbox.js"),
$.Deferred(function (deferred) {
$(deferred.resolve);
})
).done(function () {
console.log('colorbox loaded');
$('<a href="' + data.link + '" class="cleanslate custom widgetExternalPage" style="position: fixed;right:0px; top:50%; z-index:1000000000;-webkit-transform:rotate(270deg);-moz-transform:rotate(270deg);-o-transform:rotate(270deg);-ms-transform:rotate(270deg);transform:rotate(270deg);filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=3);">' + data.anchorText + '</a>').appendTo('body');
$(".widgetExternalPage").colorbox({
iframe: true,
width: "80%",
height: "80%"
});
});
});
}, true);
};