0

I have the following pages. Page A and page B.

Page A contains:

  • Page A HTML
  • Page A javascript
  • Page B javascript

I then use an ajax call to load Page B HTML into page A and fire a function to initialise page B's javascript.

If I decide to remove Page B from Page A, I will also want clear all of the JavaScript functions that were also initialised when pageB was loaded?

Is there a way to clear JavaScript functions?

2 Answers 2

4

You can use separate namespaces in both pages. So, e.g., page A places all its JavaScript under window['pageA'] whereas page B uses window['pageB'].

To unload all of the functions from page B you simply have to use

delete window['pageB'];

Beware, however, that this does not clear any handlers or references to the functions of page B. So if there are some left, this might lead to errors.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Sirko, but im not sure what you mean... in page A at the moment I have <script type="text/javascript" src="includes/javascript/pageB.js"></script> All the javascript in pageB.js is wrapped around initPageB_function(); So in this scenario how could I clear everything that is set by initPageB_function()?... kind regards J
Sorry Sirko, I don't think I explained myself very well... can you let me know how I can place all pageA's JavaScript under window['pageA']?...
I think that you are saying I should turn them into objects?... eg. pageB_object=new initPageB_function(); ... then I can delete pageB_object; ..?
@jon without knowing your code in detail, I would suggest something like the followin: window['pageB'] = { 'myFUnction': function(){ // ... }, 'myVar': 3 };. Furthermore you should place any needed global var of your functions inside this window['pageB'] object (preventing global vars would be better, but sometimes it'S easier with them, i know). The same applies to a pageA object.
thanks for your continued help... I appreciate I am being somewhat vague without code... I am trying to load page B into pageA... when page B is loaded i use function_B to initialise the click events for elements within page B... it would appear that without having to do anything that all the click events set when pageB is loaded are automatically lost when the content is replaced with something else... this is exactly what I wanted... but i thought that I would need to do something to clear them... thanks again for your time kind regards J
0

For the way you structured your code, you can simply "delete" the function initPageB_function and you should be golden, like so:

delete initPageB_function;

If you have to reload the content of pageB again into the page, then it's a different story, because you should re-bind the event handlers for your onclick events. At this point it's much way better to follow another approach:

Put the markup AND the javascript code that deals with the event handlers for pageB "into" pageB; this way, when you load pageB via Ajax you'll load also all the JS code that deals with that page; this is called delegation (and it makes perfect sense, cause your container - pageA - is not supposed to know what it is going to be loaded).

If you're using an helper library like jQuery, everything should be pretty simple:

somewhere in pageA, you define a spot for loading pageB content:

<div id='pageB'></div>

when you have to load it:

$('#pageB').load( 'http://url.for.pageB' );

As soon as the load progress, the JS code in pageB will be executed and you'll be golden :) To remove the content of the page you will simply empty the container:

$('#pageB').empty();

And the JS too will be gone. The next time you'll reload the page again, its own JS will be executed again. pretty simple and effective. :)

4 Comments

Thanks Davide... The thing that is confusing me is that if I load page B, the initPageB_function intializes the click events for all the divs loaded in pageB... however if I load pageB again I am also reloading all the click events?... does this even matter?
Nope. If you load again pageB you have to reinitialize your event handlers. In fact, I don't see any reason for you to remove the initPageB_function. My advice is just to keep the function and just call it whenever you load initPageB. When you "unload" the page, though, you have also to detach the event listeners, otherwise you'll end up having issues. I'll try to cover this in another post (in comments you cannot do that much)
I've edited my original post to show an effective solution. tell me if it suits your need.
Thanks you Davide for your continued help on this... however my testing seems to indicate that even if I have it set up as i do, that I don't seem to need to delete anything, as it would appear that the click events set when a page is loaded are automatically lost when the content is replaced with something else... which is pretty handy!... thanks again for your time, but the answer appears to be that if I initialise the click events on content loaded through ajax, these events are cleared when the loaded page is replaced. kind regards J

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.