Hello I have code which replaces document.write, makes a buffer and than pushes buffer into the document:
var lazyLoad = (function () {
var CONFIG = {
block: '',
url: ''
};
function work(){
buffer = ''
d.write = d.writeln = function(s){
buffer += s
}
d.open = d.close = function(){}
s = d.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src',CONFIG.url);
d.getElementById(CONFIG.block).appendChild(s)
s.onload = function () {
window.setTimeout(function() {
console.warn(CONFIG.block + ' ' + buffer)
d.getElementById(CONFIG.block).innerHTML += buffer;
buffer = '';
}, 0);
}
}
return {
init: function (options) {
$.extend(CONFIG, options);
window.d = document
window.onload = function(){
work()
}
}
}
})();
If I init it once:
lazyLoad.init({
url: 'http://test1.com/test1.js',
block: DIVID1
})
But if I call it again with other parameters:
lazyLoad.init({
url: 'http://test2.com/test2.js',
block: DIVID2
})
First init wont work. buffer will be empty. Where is my mistake?
asked Nov 8, 2011 at 13:03
rsboarder
4,7923 gold badges22 silver badges21 bronze badges
1 Answer 1
By calling $.extend(CONFIG, options), you're replacing the previous config with the new one.
Instead, you should create an array and append each options to it.
answered Nov 8, 2011 at 13:05
SLaks
891k182 gold badges1.9k silver badges2k bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
rsboarder
I have problems with
buffer. After first init it is empty, but after second it has some code. I think $.extend doesn't replace my buffer.lang-js