简体   繁体   中英

jquery ajax loading/unloading html+javascript

I have a simple html page

<a onclick="doSomething();">do something</a>
<script type="text/javascript">
function doSomething(){
alert("something");
}
</script>

I want to load this page using jquery ajax method , and append the result in a div. Everything works great, but I want to be able to unload the html+javascript content.

For example if I empty the div where I loaded the html content, does this will unload the javascript functions as well. I know there is a Javascript garbage collector but since there is a refference of the function doSomething connected to the link, if I delete the link did I kill the reference automatically ??

The reason why I'm asking this is because I will have more complicated scripts connected to the loaded html, and I want to be sure that if I delete the html I will delete as well the memory allocated for javascript.

Also if you know a different implementation feel free to tell me.

Sorry for my bad english.

Thanks

if you use jQuery .load() method, the resulting scripts will tag along and be parsed.

to unload the dynamically loaded JS, you need to use your own namespace that you can then null out.

App = {}  // master namespace

Then when you load a page

App.aboutPage = { ... } // your dynamically loaded namespace for the "About page"

When you're done

App.aboutPage = 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM