PDA

View Full Version : Dynamic include filenames


Thundor
08-27-2002, 02:03 PM
Here is what I am trying to do.

I dynamically build on the fly a file name along with a query string, say for example "http://www.domain.com/filename.js?var1=3&var2=6". I now need to include that JavaScript file (it resides on a remote computer because it has to interact with a database) in the code.

With ASP or PHP I can build the string server side and just use a "<script src=" to include the file. But I can't think of a way in JavaScript to include it in a similar manner.

The only solution I've even seen anyone speak of is including JavaScript in image files, but there is very little information on that (that I can find)

If anyone has any suggestions on how this might be accomplished, or any page they can send me to with more information, it would be greatly appreciated.

beetle
08-27-2002, 03:40 PM
Well, I'm not sure from your post how you want to determine the filename, but you can write <script> lines to the page with javascript, but it requires a little trick<script>
document.write('<scr'+'ipt src="filename.js"></scr'+'ipt>');
</script> See the breaks in the 'script'? That's the trick ;)

Thundor
08-27-2002, 03:50 PM
I tried your suggestion, but that code that the javascript outputs doesn't execute.

Here is the code I tried based on your suggestion.

document.write("<scr'+'ipt type=\"text/javascript\" src=\""+vFilename+"\"></scr'+'ipt>")

where vFilename is the filename I wanted included.

I also tried a Hello World example by trying to ouput a simple peice of text that way, and the code shows up, but never executes.

document.write("<script type=\"text/javascript\">document.write("Hello World");</scr'+'ipt>")

beetle
08-27-2002, 03:57 PM
This worked just fine for me<script>
document.write('<scr'+'ipt>alert("hello world");</scr'+'ipt>');
</script> You didn't split the opening script tag.

also, the problem with your first example is your concatenation is all screwy. See my post on this page (http://www.codingforums.com/showthread.php?s=&threadid=4307&highlight=concatenation+rules) about concatenation rules. Here's the fixdocument.write('<scr'+'ipt type="text/javascript" src="'+vFilename+'"></scr'+'ipt>');

Thundor
08-27-2002, 04:03 PM
Heh, works now. My newbieness is showing, the problem was in the quotes. I used double quotes where I should have had single quotes around the +vFilename+

document.write('<scr'+'ipt src=\"'+vFilename+'\"></scr'+'ipt>');

Thanks for all your help :D

beetle
08-27-2002, 04:08 PM
You're welcome. but I should note that in your post above, it is not necessary to escape the double-quotes in that line. Since you are using single-quotes for your string containers, double-quotes can be used regularly within the string.

If you haven't yet, you should visit the post I linked to and read my 'rules' :D

beetle
08-27-2002, 04:26 PM
Oh, and I also feel obligated to note that this can be done with the DOM as well, and even turned into a function and changed dynamically while at the page...var head = document.getElementsByTagName('head').item(0);
script = document.createElement('script');
script.src = 'filename.js';
script.type = 'text/javascript';
head.appendChild(script);