I am trying to append a script from another javascript file, I have followed this post on non-blocking async script loading http://calendar.perfplanet.com/2012/the-non-blocking-script-loader-pattern I have been able to load the external script but I am unable to instantiate an object in the second js file, here is the code:
(function(url, callback){
var dom,doc,where,iframe = document.createElement('iframe');
iframe.src = "javascript:false";
iframe.id = 'iframeTest';
(iframe.frameElement || iframe).style.cssText = "border: 0px; z- index: 50; position: fixed; height: 290px; width: 225px; right: 0px; bottom: 0px;";
where = document.getElementsByTagName('script');
where = where[where.length - 1];
where.parentNode.insertBefore(iframe, where);
try {
doc = iframe.contentWindow.document;
} catch(e) {
dom = document.domain;
iframe.src="javascript:var d=document.open();d.domain='"+dom+"';void(0);";
doc = iframe.contentWindow.document;
}
doc.open()._l = function() {
var head = document.getElementById('iframeTest').contentWindow.document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onload = callback;
head.appendChild(script);
}
doc.write('<body onload="document._l();">');
doc.close();
})
('http://localhost/js/chat.js', function() { var cb = new Chatbox('6'); cb.init();} );
And in the chat.js file that I am trying to load:
function Chatbox(orgId)
{
this.orgId = orgId;
}
Chatbox.prototype.constructor = Chatbox;
Chatbox.prototype.init = function() {
this.getStatus();
};
.....................................
When I try to load the page , I end up getting error saying 'Uncaught ReferenceError: Chatbox is not defined'. I am aware there are similar questions here but couldn't find a solution for instantiating an object in the second file, though it works if its not async. Hope I am clear and any help will be highly appreciated. Thanks in Advance!
Chatbox.prototype.constructor = Chatbox;; But it's not gonna help anyway I guess)). Let me have a look at it