Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to clear JS browser store 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I've been playing with AJAX and trying to dynamically load JS, I then tried to simply alter the src of a script tag, which works fine, however..

If you have a function called test1() in script test1.js, you load it by default on the page.

In that function it changes the source of the script tag to test2.js and then fires test2() it works fine.

However, you can still call test1() even though the source of the script has been altered.

In fact I've even changed the source to = '' , and both functions are still available to be called.

Therefore changing the src of a JS tag doesn't change the functions available to the browser, but must add them to some repository.

As it behaves in this manner, you might as well load all JS files at the start.

I'm trying to find a way to load the JS in small chunks and only use up resources to store the JS required for the section of the application they are currently using.

Is this possible?

I was even thinking of creating an object and changing it's source where the methods were stored within the object.

would creating a class with methods and assigning it to an object be the only way to achieve what I want or is there a way to clear the JS store?

Cheers,

1DMF.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi

As far as I know, you have no control over that.

However you can redefine things in JavaScript. I would expect this to remove the object from the memory :
Code:
test1[teal]=[/teal]undefined[teal];[/teal]

Feherke.
 
So I gues the only true way to dynamically load JS and not continue to use up memory would be to learn how to create Objects , Classes and JavaScript prototyping.

hmm, nothing is ever easy is it, well until you know how ;-)

Thanks Feherke.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
You can use the little-discussed 'delete' command to remove all references to anything, whether it be a function, a string, whatever.

So this:

Code:
function wibble() {
   alert('You are running wibble.');
}

wibble();
delete wibble;
wibble();

will run the function once, and then remove all reference to it.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks guys, only this is on a function by function basis.

I was hoping to dynamically load an external JS file that would have loads of functions, and remove them adhoc.

Is there a way of dynamically collecting all functions names in a script to loop and delete that way?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Is there a way of dynamically collecting all functions names in a script to loop and delete that way?

Yes - JSUnit manages it quite nicely. My advice is to download JSUnit, and take a peek under the hood, probably in app\jsUnitTestManager.js around line 257 to 300.

If it's not those lines, you'd need to debug it a bit more than my 10-second cursory glance :)

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top