I opened a javaScript file in a javaScript time....
document.write("<script src='newnote.js' type='text/javascript'></script>");
is there an other way to load the js in js code..?
(this file is for loading a popup menu js code , which is loaded after delay by clock js code ... so i want an othe way to loaded it)
I opened a javaScript file in a javaScript time....
document.write("<script src='newnote.js' type='text/javascript'></script>");
is there an other way to load the js in js code..?
(this file is for loading a popup menu js code , which is loaded after delay by clock js code ... so i want an othe way to loaded it)
Share Improve this question edited Dec 14, 2009 at 13:51 Rich Bradshaw 73.1k46 gold badges188 silver badges241 bronze badges asked Dec 14, 2009 at 7:39 jjjjjj 6051 gold badge9 silver badges26 bronze badges 6- 1 By close, do you mean remove it from the page? The next logical question is why do you need to remove it? – Doug Neiner Commented Dec 14, 2009 at 7:43
- You could remove the reference from the document to that resource. – Gumbo Commented Dec 14, 2009 at 7:46
- Have you tried document.close() what happened – ACP Commented Dec 14, 2009 at 7:47
- Above script gives javascript error quotes problem – valli Commented Dec 14, 2009 at 7:50
- 1 @jjj: The reason everyone's so confused by what you are trying to do is that Javascript files aren't meant to be nor can be unloaded from the current page. Even if you remove the reference to the file, all the script has already been parsed so everything remains in memory and is still accessible from other scripts or events. – Andy E Commented Dec 14, 2009 at 12:04
5 Answers
Reset to default 6When opening a JS file, its code is executed at once - functions are created, code is run, events are set. The only way to 'unload' a Javascript file is to manually undo all the code that has been run as a result of loading the unwanted file: setting all new functions, variables and prototype aditions to undefined
(e.g. window.badFunction = undefined
, unset all events, remove all new DOM elements..
If you wanted to unload another JS file every time when opening a page, it could in theory be done, but not very easily and if the loaded JS file should change, you would have to update your invalidating file.
What you did isn't like opening a local file in a programming language like C++ or Java. You don't need to close anything.
Is it possible that the script that you are adding to the page (in this case newnote.js
) is causing the error you are experiencing?
Instead of the line you used starting with document.write
use this instead:
var newnote = document.createElement('script');
newnote.src = "newnote.js";
newnote.type = "text/javascript";
document.documentElement.firstChild.appendChild( newnote );
If you still get your quotes error, then the code inside of newnote.js
is messed up.
Don't think this was really what you were asking, but if you used the code I listed above you could then remove this file from your page by calling this:
document.documentElement.firstChild.removeChild( newnote );
One more thought:
If your path to newnote.js
is not correct (because it is not in the same directory as the calling page) then the server would return a 404
error page instead of the file. If your browser tried to execute it like javascript, it could throw an error. Try supplying the full URL: http://yoursite./js/newnote.js
or a root relative one: /js/newnote.js
you are not opening a javascript file, in fact you cannot open any file from local file system, so there is no question of closing. You are inserting a script tag replacing all contents of the document. This would result in fetching newnote.js using the current url with newnote.js replacing anything after last slash.
To include a script you could try this:
var s = document.createElement('script');
s.src = 'newnote.js';
s.type = 'text/javascript';
var head = document.getElementsByTagName('head')[0]
head.appendChild(s);
or like that:
document.write("<script type='text/javascript' src='newnote.js'><\/sc" + "ript>");