V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
a7898585
V2EX  ›  程序员

js 文件里写 js 代码问题

  •  
  •   a7898585 · 2014-09-17 21:20:40 +08:00 · 3686 次点击
    这是一个创建于 3480 天前的主题,其中的信息可能已经有所发展或是发生改变。
    做了个小站,申请的谷歌和百度的广告,
    放到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>");
    6 条回复    2014-09-19 09:55:22 +08:00
    sneezry
        1
    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
        2
    kmvan  
       2014-09-17 21:58:59 +08:00
    为啥要这样写,就算是 document.write ,也会堵塞页面的
    acecode
        3
    acecode  
       2014-09-18 00:19:49 +08:00
    document.write才对吧
    另外 浏览器检查 script标签内容的时候看到</script>会当做脚本定义完毕
    参照 h5bp 的做法,字符串里应该用这个 <\/script>
    hkongm
        4
    hkongm  
       2014-09-18 08:55:42 +08:00
    async或者defer下吧

    另外,小声的说下,楼主是不是忘了关adb?
    a7898585
        5
    a7898585  
    OP
       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
        6
    cdxem713  
       2014-09-19 09:55:22 +08:00
    这写法还是够奇葩
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3103 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:30 · PVG 22:30 · LAX 07:30 · JFK 10:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.