admin管理员组文章数量:1290935
In less words:
I have multiple JavaScript sections located at different places in document. Some of them are loaded and evaluated with AJAX:
<script id="js5533" type="text/javascript">
javascript and jquery stuff (lots)
</script>
<script id="js7711" type="text/javascript">
javascript and jquery stuff (lots)
</script>
As you can see I ID every section.
My goal is to get rid of specific section by clicking on button.
$('.somebutton').click(function() {
$('#js7711').remove();
});
Of course this function removes only element from the document and all JavaScript functions in section js7711 are still working.
I don't want to manually remove every function and variable from the section by doing things like a(); a = 0;
Are there any less coding solutions?
Thanks.
P.S. jQuery is also used.
In less words:
I have multiple JavaScript sections located at different places in document. Some of them are loaded and evaluated with AJAX:
<script id="js5533" type="text/javascript">
javascript and jquery stuff (lots)
</script>
<script id="js7711" type="text/javascript">
javascript and jquery stuff (lots)
</script>
As you can see I ID every section.
My goal is to get rid of specific section by clicking on button.
$('.somebutton').click(function() {
$('#js7711').remove();
});
Of course this function removes only element from the document and all JavaScript functions in section js7711 are still working.
I don't want to manually remove every function and variable from the section by doing things like a(); a = 0;
Are there any less coding solutions?
Thanks.
P.S. jQuery is also used.
Share Improve this question asked Feb 4, 2012 at 20:38 scorpio1441scorpio1441 3,09810 gold badges41 silver badges79 bronze badges 12- What kind of code do you have there? Why do you want to get rid of it? – Sergio Tulentsev Commented Feb 4, 2012 at 20:40
- 2 What are you trying to achieve with that? – ShankarSangoli Commented Feb 4, 2012 at 20:40
- what you are trying to achieve can only be made if you control the server side code that is serving the ajax contents. If not, there is no way to uninstantiate what is inside thouse javascript file unless you know exactly what are the names of every function inside them – André Alçada Padez Commented Feb 4, 2012 at 20:43
- @Sergio Tulentsev: Different. jQuery animations, regular JavaScript, just any. – scorpio1441 Commented Feb 4, 2012 at 20:44
- @André Alçada Padez: I surely control server side code. – scorpio1441 Commented Feb 4, 2012 at 20:48
4 Answers
Reset to default 3Despite i find this question really weird I will try no to go deeper and just anwser. Rewrite your imported scripts to something like that:
window.js7711 = function() {
// javascript and jquery stuff (lots)
}
Then when you load this awkward thing you should execute it js7711()
And on deletion I assume you do something like:
var myID = "js7711"
$("#" + myID).remove();
add this:
delete window[myID]
If you declare functions in this imported script you should think of adding an array with imported functions names js7711.imported = ["something", "trololo"]
and then remove them in a loop.
Anyhow I think your approach/structure is kinda messed up.
Found a lazy workaround for this.
First of all: reason for getting rid of previous JS code is - to run a new code (similar or same with the old one) without reloading page.
The solution is:
To forget about previously loaded code and execute your new code with jQuery.globalEval(js);
where js is a var containing your actual new JS code.
jQuery just creates another sequence with your new code which does not interfere with the old one excluding live events (solved with event.stopPropagation())
Works for me perfectly.
Like someone said: you cannot make a browser forget about the JS which was already executed.
Are there any less coding solutions?
Nope.
You better think again if it's really the best approach, I mean get rid of javascript sctions...
The problem indeed is that if you had bound any events to elements outside #js7711, these will still call, i.e. the javascript functions still work.
The solutions that es to mind is to generate JS functions that check if their 'root' element (i.e. #js7711) still exists, and only execute if the element can still be found.
Then, if you would remove #js7711 the JS functions would stop working, probably what you want!
本文标签: jqueryHow to remove JavaScript loaded dynamically with AJAXStack Overflow
版权声明:本文标题:jquery - How to remove JavaScript loaded dynamically with AJAX - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503426a2382181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论