View Full Version : include or not to include
cslotek
01-31-2003, 09:49 PM
hi all
already been pullin my hair... there's almost none left
basically what i'm trying to do is:
when the user selects an option from a drop-down, i'm checking the value and wanting to include a .js file from another location based on that value. It's necessary to access one of more than several of these files, so i can't just load em into the page first thing, because it would take way too long.
Is it possible to include a remote .js file within a javascript (all within the same web)? Or do i have to break the script up into little pieces in order for the browser to comprehend?
any help is greatly appreciated... much thanks in advance
chris
BrightNail
02-01-2003, 07:53 AM
hmmm....what you want to do is 'render' a js include AFTER the page has loaded...no --> you can't do that....what you can do is..use the dropdown on a previous page...and carry that thru the url string...on the subsequent page, read the value that you passed and then write out the appropriate js include...
other than that....I don't see how you can render a js file AFTER the page has loaded...you'd have to 'refresh' the page and pass the dropdown selection thru the url....so it can be read on refresh...
cslotek
02-01-2003, 08:38 AM
thanks for the reply
heh, i was tryin not to go that route but i will if i have to.
Today i was able to kind of step thru the code and get what i needed by checking each value as it's selected. I would just take that value and include the "script" that the corresponded with the value (well, i say script but i'm really pulling text that fits into the script to display information regarding their selection- i just named it *.js). And that method works ok. But since all the many inclusions would have to be accounted for in the first script i was sure there had to be a shortcut, maybe some concatenation technique where i can just insert the value into the include, since the names of the files correspond with the selected values. Checking for each one would be terribly inefficient.
(ie: like this sorta? ideally? summing up the task at hand...
-user selects an option from drop down.
-onchange i call a function which grabs the value from the selected index
-i (theoretically) take the value and dynamically fit it into an include script like so:
<script type="text/javascript" src="value.js"></script>
-and voila the information appears that the user wants to see, like magic right? but without the page reload
)
ah well, i still have a few hairs left;)
cheers
chris
Adam20002
02-01-2003, 01:01 PM
would this do :-
var s = document.createElement("SCRIPT");
s.setAttribute("SRC","filename.js");
document.appendChild(s);
type thing ? Basically using the DOM you can dynamically write a new script tag to the page with whatever you want as the .js file name.
Adam
piglet
02-01-2003, 03:05 PM
Yes - that's the route I'd take (if I wasn't too worried about having to support IE4/5.0 or Netscape 4).
When loading scripts it's quite a good idea to use IDs for the script and load and unload into the same ID rather than just keep appending to the end of the document - you could end up with more than one copy or end up with two versions of the same function name...
There's an example of this sort of thing on this link (http://www.webxpertz.net/forums/showthread.php3?s=&threadid=19203) - I hope it's of interest.
cslotek
02-01-2003, 07:46 PM
thanks for the ideas- i think i have something to work with now!
chris:D
BrightNail
02-02-2003, 03:37 AM
hmmm,
this is interesting as I tend to code for 4.0 and higher browsers, so many of the 'recent' methods in javascript I am not familiar with..yet, they are very powerful.
maybe I should get out of the stone age....
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.