如何删除加载的脚本和添加新的脚本
How to remove loaded script and add new script?
我正在创建一个演示,我在URL中添加了一个查询字符串。当我加载我的HTML,我得到我的查询值。我想让它,如果查询字符串的值为"简单",它删除JS文件加载。
如果它发现一个值不是"simple",它应该加载另一个JS文件。
我用谷歌搜索了一下,但没有任何效果。
这是我的HTML:<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="js/copynode.js" type="text/javascript" id="dynamic_files"></script>
<script type="text/javascript">
$(window).load(function(){
// full load
var query = decodeURIComponent(window.location.search.substring(1));
console.log("---------------------");
var index=query.indexOf("=")
query=query.substr(index+1,query.length);
console.log(query);
if(query=="simple"){
alert('if--');
//$('#dynamic_files').remove();
}else{
alert('else--');
$('#dynamic_files').remove();
var script = document.createElement('script');
script.src = 'js/index.js';
document.getElementsByTagName('div')[0].appendChild(script);
}
});
</script>
</head>
<body >
<h1>My Web Page</h1>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="loadScript()">Load Script</button>
</body>
</html>
您可以先检查String
的值,然后编写如下函数:
function checkString(){
if($('#ID_OF_ELEMENT_OF_THE_STRING').value == "simple"){
//load the script needed
var head = document.getElementsByTagName('head').item(0);
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'http://mysite/my.js');
head.appendChild(script);
}else{
//load the other part.
}
}
相关文章:
- 如何将脚本添加到<头部>标记
- 将Google Adwords脚本添加到Vaadin应用程序中
- 如何以编程方式将内容脚本添加到 iframe 文档
- 使用java脚本添加图像进行测验
- 我如何向这个脚本添加一个点击按钮循环URL函数(按给定顺序在URL之间切换),或者这可能吗
- 我在向jquery脚本添加延迟时遇到问题
- 如何使用java脚本添加链接
- 如何将自定义脚本添加到运行javascript文件的package.json文件中
- 取消绑定由加载jQuery'的外部脚本添加的事件;s的getScript函数
- 如何最好地将跟踪脚本添加到您的网站
- 如何将谷歌地图脚本添加到wordpress帖子中
- 如何让我的重定向脚本添加一个随机后缀来选择网站
- 星云脚本添加在 EasyPHP 上
- ASP.Net 将脚本添加到脚本管理器时,更新进度中断
- 从脚本添加的输入框中获取值
- 向此 jQuery 脚本添加动画
- 如何使用我的 jQuery 日期选择器脚本添加 dd-mm-yy 的日期格式
- 使用脚本添加两个同名的数组值
- 让 Modernizr.load() 将脚本添加到页面末尾
- 将脚本添加到 asp.net 内容页面