开发者

What happens after removing an external js file

开发者 https://www.devze.com 2023-02-17 05:31 出处:网络
<!-- Add-in device.js files are placed here --> <div id=\"deviceScript\"></div> What I have been doing is clearing this out when I select a new (add-in) device and append a script
<!-- Add-in device.js files are placed here -->
    <div id="deviceScript"></div>

What I have been doing is clearing this out when I select a new (add-in) device and append a script tag with its .src file.

I have some vague idea that unhooking an external js file might remove its code from memory. If not is there a way to do so?

Functions in different script file开发者_如何学JAVAs have the same name - for example, I use start() to set up each device. It seems that the latest device start() overwrites the one in memory - but I am not sure if overwrite means delete last one or whether there is some horrendous build up of dross going on.

Any clarification appreciated.


If you remove the javascript file and the webpage is already loaded, the code is still in memory (at least the browser can access to it) so you can still 'use' it. If you reload/refresh the page the code will not be accessible and hence you will cannot use it. I think that there is no way of deleting this code from memory (or from wherever it is accessible for the browser so far).

It is not a good practice to write the same name of a function in several js files if you wish to use them all in a certain page. The function defined in the last stated js would be used, so if you call to that function twice (start() in your case) your function() will do the very same.

In case of writing the same name to two functions on the same js, it works similarly. Whilst in other languages you might have myfunc(oneparm) and myfunc(parmone, parmtwo) as two separate functions with the one that gets run depending on the number of parameters passed, in JavaScript the last one defined will always be the one run regardless of the number of parameters.

Keep in mind javascript is a client-side scripting language


Here's what I think might be an answer but perhaps it is really another question - is this logical?

The context: Visitor selects a named object to display a set of keys (say one is Color). Selection of a key displays one or more device names (say setColor). Selection of a device name displays a set of options (say Red, Green, Blue).

In the second step I swap the previous device.js with a new device.js. The js has time to load before the selection of the option calls a function within it to make the action.

So if any of the function names duplicates what has gone before, they overwrite them because of the swap action. This means I don't have to worry about naming conventions other than the name of the main function is the same as the device name ... devices could be made by anyone. Perhaps it would be a good idea to reload every now and again.

Any thoughts? Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消