[英]How to change add javascript files dynamically in the header of an html file?
I have an html page, I need to add some reference to JS files in the head of the html. 我有一个html页面,我需要在html的头部添加一些JS文件的引用。 The following code is working, but the scriptTVKeyValue is always being added before the tag I would like to add instead directly after Any idea what I am doing wrong here? 下面的代码是有效的,但是在我想要添加的标签之前,始终会添加scriptTVKeyValue,而不知道我在这里做错了什么?
<head>
// I want reference added here
<script src="js/jquery.js"></script>
<script src="js/json2.js"></script>
// Reference to file added here
</head>
// APP_MAIN.onLoad()
var scriptTVKeyValue = document.createElement('script');
scriptTVKeyValue.type = 'text/javascript';
scriptTVKeyValue.src = '$MANAGER_WIDGET/Common/API/TVKeyValue.js';
head.appendChild(scriptTVKeyValue);
Solution using jQuery : 使用jQuery的解决方案:
//Take the refference to the previous node
var $jsFile = $container.find('[src="js/jquery.js"]');
var fileName= '$MANAGER_WIDGET/Common/API/TVKeyValue.js';
// Insert the script
$jsFile .insertAfter($('<script>')
.attr('type', 'text/javascript')
.attr('src', fileName));
Look at http://www.jspatterns.com/the-ridiculous-case-of-adding-a-script-element/ 请查看http://www.jspatterns.com/the-ridiculous-case-of-adding-a-script-element/
You should have the answer 你应该有答案
如果您很高兴/允许使用JS库,那么您可以使用requireJS加载脚本它具有执行此操作的高级功能:它将使您能够在加载动态添加的脚本后调用函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.