document.write动态加载脚本

Jac*_*ack 0 html javascript jquery

我正在尝试动态加载脚本.这是我的代码:

document.write('<script type="text/javascript">window.jQuery || document.write(\"<script src=\'http://code.jquery.com/jquery-1.10.2.min.js\'></script>\")</script>');
Run Code Online (Sandbox Code Playgroud)

这是在B.js里面,它作为脚本加载到A.html上.但我得到错误.双引号内的结束脚本标记被视为嵌套脚本标记并破坏代码.

任何想法如何解决这一问题?

Mil*_*war 5

使用:

var s = document.createElement("script");
s.type = "text/javascript";
s.src = "http://somedomain.com/somescript";
    $("head").append(s);
//or using plain js
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(s);
Run Code Online (Sandbox Code Playgroud)

动态负载的jquery库JavaScript的

  • 我喜欢你如何_vanilla_到'$("head")`xD [`document.head`](https://developer.mozilla.org/en-US/docs/Web/API/document.head) (3认同)