开发者

Workaround document.write function

I'm trying to find a way to ease my users life (by avoiding render blocking) by removing all document.write() that could be made by ads scripts.

The orignal works like this :

var ad_tmstp=Math.round(Math.random()*10000000000),
sas_masterflag=1;

function adscript(ad_pageid,ad_formatid,ad_target)
{
 var scripts = document.getElementsByTagName( 'script' );
 var currentScript = scripts[ scripts.length - 1 ];

 if (ad_masterflag==1) {ad_masterflag=0;ad_master='M';} else {ad_master='S';};

document.write('<scr'+'ipt src="http://www.ads.com/call/pubj/' + ad_pageid + '/' + ad_formatid + '/' + ad_master + '/' + ad_tmstp + '/' + escape(ad_target) + '?"></scr'+'ipt>');
}

This is the master ads function, used to generate unique ad url, document.write() it to the DOM, and make he response execute.

The call to this master function is made in site, where ad needs to be inserted:

<script type="text/javascript">
  ad_pageid='16645/120305'; 
  ad_formatid=7366;     
  ad_target='';         
  adscript(ad_pageid,ad_formatid,ad_target);
</script>

When page loads, master script is executed, causing document.write() to happen. Dynamic script is loaded, and is itself executed and ends with a nasty

  [...]Ad innerCode creation[...]
  if(typeof(ad_ajax)!='undefined'&&ad_ajax)  
  {ad_appendToContainer(1234,innerCode);}else{document.write(innerCode);}

Nasty enough to be seriously watched...

So, what i've been trying so far :

var script = document.createElement('script');
  script.type = 'text/javascript';
  script.defer= "true";
  script.src  = 'http://www.ads.com/call/pubj/' + ad_pageid + '/' + ad_formatid + '/' + ad_master + '/' + ad_tmstp + '/' + escape(ad_target) + '?';
  currentScript.parentNode.insertBefore( script, currentScript.nextSibling );

HTML rendering is perfect : #1 creates #2. But, for some reason, #2, containing the final document.write() wich should generate ad content, doesn't execute.

What could I possibly be missing ? 开发者_运维技巧Thanks.


If you want to eliminate all document.write() calls, just do this:

document.write = function() {};

and each document.write() call won't do anything :)

(maybe i just misunderstood you because it's this simple ;))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜