我在这里结束了伙计们。我正在尝试加快我们主页的加载时间。有两个广告因为速度测试和可用性而扼杀了我们。广告代码是带有 in script 标签的脚本标签,如下所示:
<script type="text/javascript">if(typeof(cachebuster) == "undefined"){var cachebuster =
Math.floor(Math.random()*10000000000)} if(typeof(dcopt) == "undefined"){var dcopt =
"dcopt=ist;"} else {var dcopt = ""} if(typeof(tile) == "undefined"){var tile = 1}
else
{tile++} document.write('<scr'+'ipt src="AD_URL;' + dcopt + ';tile=' + tile +
';sz=728x90;ord=' + cachebuster + '?"></scr'+'ipt>');
</script>
<noscript>
<a href="AD_URL" target="_blank" ><img src="AD_URL" border="0" alt="" /></a>
</noscript>
这是其中一个广告的原始代码。第二个非常相似。这需要永远加载。有时长达10-15秒。不酷。
我试图做这样的事情:
$('#HeaderAd').append(function(){
var ad = '<"scr" + "ipt " type="text/javascript">if(typeof(cachebuster) == "undefined"){var cachebuster = Math.floor(Math.random()*10000000000)} if(typeof(dcopt) == "undefined"){var dcopt = "dcopt=ist;"} else {var dcopt = ""} if(typeof(tile) == "undefined"){var tile = 1} else {tile++} "</scr"+"ipt>";';
var ad2 = 'document.write("<scr"+"ipt " src="http://ad.doubleclick.net/adj/ohn.auctionarms/shooting_sports_home;pos=atf_3;" + dcopt + ";tile=" + tile + ";sz=728x90;ord=" + cachebuster + "?">"</scr"+"ipt>");';
var ad3 = '<noscript><a href="http://ad.doubleclick.net/jump/ohn.auctionarms/shooting_sports_home;pos=atf_3;sz=728x90;ord=123456789?" target="_blank" ><img src="http://ad.doubleclick.net/ad/ohn.auctionarms/shooting_sports_home;pos=atf_3;sz=728x90;ord=123456789?" border="0" alt="" /></a></noscript>';
var ad_ = document.createElement('script');
var ad_2 = document.createElement('script');
ad_.type = 'text/javascript';
ad_2.src = "http://ad.doubleclick.net/adj/ohn.auctionarms/shooting_sports_home;pos=atf_3;' + dcopt + ';tile=' + tile + ';sz=728x90;ord=' + cachebuster + '?";
ad_.innerText = 'if(typeof(cachebuster) == "undefined"){var cachebuster = Math.floor(Math.random()*10000000000)} if(typeof(dcopt) == "undefined"){var dcopt = "dcopt=ist;"} else {var dcopt = ""} if(typeof(tile) == "undefined"){var tile = 1} else {tile++} ';// + 'document.write('+ad_2.src+');';
ad_.appendChild(ad_2);
console.log(ad_);
return ad_;
});
但这给了我 jquery 错误并且不会加载。广告不会加载。如果我尝试只用一个“document.createElement”来做所有事情,那么它仍然不喜欢它。
我做错了吗?有没有更好的方法来异步加载这些广告?