1

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

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.