09-23-2005, 02:56 PM
I know this can be done in HTML using <script src="something.js"></script>.
I need to dynamically have access to a function in a file but only when a certain JS method in a certain class needs it. I don't want to have to include it everytime in the HTML code when it is used to rarely (mainly for debugging).
09-23-2005, 02:59 PM
there are several ways to add a script dynamically, but you must keep in mind that you will not get "instant" access to it. it needs to be loaded into memory of the browser and properly parsed by script engine, before made available, and this type of "late-binding" could interupt your variables, blah...
you can also, append a script and add event listener to let you know when that particular script is loaded to the page. (a prefered method)
This is a pretty ugly idea, but it may work:
create an IFRAME element on your page like this:
<IFRAME ID="Script" NAME="Script" src="about:blank" style="display:none"></IFRAME>
now, when you have your function and you want to access an outside .js dynamically, do this:
document.all.Script.Function/Var/Object/etc. = Bleh;
Like I said, Not a pretty idea, and there are most likely better ones, but I know this works. (unless its a separate domain).
09-23-2005, 06:18 PM
/// imports a script into the document
/// script reference is appended to the calling script tag
This works, because the <script> tag is appended to the calling script tag. So that the browser can parse it after it finished parsing the calling script tag.
09-29-2005, 08:47 PM
I'll try that and see if I can get it to work.
Thanks for the help!
09-30-2005, 11:27 AM
var script = document.createElement("script")
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.