删除附加脚本 javascript [英] remove appended script javascript
问题描述
我如何删除我附加的脚本,因为它会导致我的应用出现一些问题.
这是我获取脚本的代码
var nowDate = new Date().getTime();var url = val.redirect_uri + "notify.js?nocache=" + nowDate + "&callback=dummy";var script = document.createElement('script');脚本.src = url;document.body.appendChild(script);
然后我有一个自动加载功能,这会导致创建另一个元素脚本.
我想删除在添加另一个元素之前附加的前一个元素.
我又做了一些测试,在你得到问题的正确答案之前(希望有一个),你可以试试这个:
<脚本>(函数 foo(){var b=function moo(){var c=document.getElementsByTagName('script');警报(document.body.innerHTML);c[0].parentElement.removeChild(c[0]);警报(document.body.innerHTML);}var a=setTimeout(b,1000);b=空;})();富=空;
这只是一个测试代码,但它包含了一个想法,你可以如何解决这个问题.它从 DOM 中删除
,最后一行破坏了脚本的所有功能.
(代码也有一点细节,这表明,setTimeout
会做 eval()
,不管它是如何论证的......?)>
how can I remove my appended script because it causes some problems in my app.
This is my code to get my script
var nowDate = new Date().getTime();
var url = val.redirect_uri + "notify.js?nocache=" + nowDate + "&callback=dummy";
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script);
Then I have an auto load function, that causes to create another element script.
I want to get rid of the previous element that was appended before another element is added.
I did some more tests, and before you'll get a correct answer to your question (hope there is a one) you can try this:
<button onclick="foo()">ShowHTML</button>
<script>
(function foo(){
var b=function moo(){
var c=document.getElementsByTagName('script');
alert(document.body.innerHTML);
c[0].parentElement.removeChild(c[0]);
alert(document.body.innerHTML);
}
var a=setTimeout(b,1000);
b=null;
})();
foo=null;
</script>
This is just a test code, but it contains an idea, how you possible could solve the problem. It removes <sript>
from the DOM, and the last line destroys all functionality of the script.
(The code also has a little detail, which shows, that setTimeout
will do eval()
, no matter how it is argumented...?)
这篇关于删除附加脚本 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!