javascript 加载一次 show_ads.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10842382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
load show_ads.js once
提问by 422
So we run google ads on our sites.
所以我们在我们的网站上运行谷歌广告。
And I got thinking, each ad block ( of various sizes ) loads ..
我开始思考,每个广告块(各种尺寸)加载..
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
eg:
例如:
<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxxxxxxxxxxxxx";
/* ad served */
google_ad_slot = "xxxxxxxxx";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
and other ads, of different slot ( id's ) amd sizes, on the same page also load :
和其他不同广告位 (id's) 和尺寸的广告,在同一页面上也会加载:
http://pagead2.googlesyndication.com/pagead/show_ads.js
each time. So a page with say three ads on, loads that exact same js file 3 times ..
每一次。所以一个页面上有三个广告,加载完全相同的 js 文件 3 次..
Removing that script file, adding it ONCE to the head, doesnt work for us.. the ads just dont show.
删除该脚本文件,将其添加到头部一次,对我们不起作用......广告只是不显示。
So is there a method of loading that show_ads.js file just once / page load ?
那么是否有一种方法可以只加载一次/页面加载该 show_ads.js 文件?
回答by josh3736
The Google ads script utilizes document.write()
, which means the script must be in the position in the HTML that you want the ad.
Google 广告脚本使用document.write()
,这意味着脚本必须位于 HTML 中您想要广告的位置。
However, fear not: the show_ads.jsfile will only be downloaded by your browser once. The subsequent <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
s will load from cache.
但是,不要担心:show_ads.js文件只会被您的浏览器下载一次。后续<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
s 将从缓存加载。
In fact, the Cache-Control
headers tell your browser it can load the file directly from cache for the next hour, so there should be only one trip to pagead2.googlesyndication.comper session, no matter how many pages are viewed.
事实上,Cache-Control
标题告诉您的浏览器它可以在下一小时直接从缓存加载文件,因此无论查看多少页面,每个会话都应该只访问pagead2.googlesyndication.com一次。