js 文件里写 js 代码问题

2014-09-17 21:20:40 +08:00
 a7898585
做了个小站,申请的谷歌和百度的广告,
放到js文件中后,用document.write来写的,但是前台就是不出现,请帮忙看下是哪里出错了?

document.writeln("<\script src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 970*250 -->
<ins class="adsbygoogle"
style="display:inline-block;width:970px;height:250px"
data-ad-client="ca-pub-9055653551438880"
data-ad-slot="7443996645"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>");
3721 次点击
所在节点    程序员
6 条回复
sneezry
2014-09-17 21:52:03 +08:00
var ga = document.createElement("script");
ga.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
document.getElementsByTagName("head")[0].appendChild(ga);

var ins = document.createElement("ins");
ins.className = "adsbygoogle";
ins.style = "display:inline-block;width:970px;height:250px";
ins.setAttribute("data-ad-client", "ca-pub-9055653551438880");
ins.setAttribute("data-ad-slot", "7443996645");
document.getElementsByTagName("body")[0].appendChild(ins);

(adsbygoogle = window.adsbygoogle || []).push({});



虽然不知道楼主为啥不直接写进HTML,但是楼主的写法太尼玛凶残了……
kmvan
2014-09-17 21:58:59 +08:00
为啥要这样写,就算是 document.write ,也会堵塞页面的
acecode
2014-09-18 00:19:49 +08:00
document.write才对吧
另外 浏览器检查 script标签内容的时候看到</script>会当做脚本定义完毕
参照 h5bp 的做法,字符串里应该用这个 <\/script>
hkongm
2014-09-18 08:55:42 +08:00
async或者defer下吧

另外,小声的说下,楼主是不是忘了关adb?
a7898585
2014-09-18 09:29:53 +08:00
@sneezry
@hkongm
@acecode
@kmvan
改好了,谢谢各位
document.writeln('<script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></scr'+'ipt>'
+' <!-- 970*250 -->'
+' <ins class="adsbygoogle"'
+' style="display:inline-block;width:970px;height:250px"'
+' data-ad-client="ca-pub-9055653551438880"'
+' data-ad-slot="7443996645"></ins>'
+' <script>'
+' (adsbygoogle = window.adsbygoogle || []).push({});'
+' </scr'+'ipt>');
cdxem713
2014-09-19 09:55:22 +08:00
这写法还是够奇葩

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/134062

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX