[英]Jquery Script not loading in head when I create dynamically html page using javascript
I created dynamically new window in JavaScript and add some HTML code in it through JavaScript, but when I insert some script link into html head, it does not load when window is open. 我在JavaScript中动态创建了一个新窗口,并通过JavaScript在其中添加了一些HTML代码,但是当我在html head中插入一些脚本链接时,当窗口打开时它不会加载。
<script type="text/javascript">
function newWindowMap(){
var myWindow = window.open('','_blank');
var head = myWindow.document.getElementsByTagName('head')[0];
var body = myWindow.document.getElementsByTagName('body')[0];
var jqueryScript = myWindow.document.createElement('script');
jqueryScript.src = 'jquery.min.js';
jqueryScript.type = "text/javascript";
head.appendChild(jqueryScript);
var alertScr = myWindow.document.createElement('script');
var inlineScript =
document.createTextNode('$(document).ready(function(){alert("Hello World!");});');
alertScr.appendChild(inlineScript);
body.appendChild(alertScr);
}
</script>
Error on console: 控制台错误:
Uncaught ReferenceError: $ is not defined at :1:1
未捕获的ReferenceError:$未定义为:1:1
$
is from JQuery, which is a popular library for JavaScript, and from the looks, you haven't imported it. $
来自JQuery,它是JavaScript的流行库,从外观上来说,您尚未导入它。
Add this into your head tag to fix the issue 将此添加到您的头部标签即可解决此问题
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.