PDA

View Full Version : Link JS file with a variable (3 questions)


Clearline
07-18-2003, 07:22 AM
1)
I want to link a js file(s), dynamically, by using a var:

VAR=//current.js
<script language="javascript" src= VAR ></script>

I have tried every way to write this I can think of, including using &{VAR}; brackets, but can not get the js file to take.

2)
I am using a frame structure, and each file loaded into a frame or later targeted into a frame has to be linked to the JS in use, in its header.
I was hoping that by linking the JS to the top.Frameset, that all subsequent files would use the same JS.
(Again this has to do with dynamically linking to different scripts)

3)
I tried to load a framset, by declaring the frames / names, then tried to use a java script to fill the frames, but was not successful. I guess I could load the frames blank, and then have the last frame to load do an onload fill of the targetted frames. Can it be done directly from the frameset load with JS?
:rolleyes:

glenngv
07-18-2003, 07:45 AM
<head>
<script type="text/javascript">

document.write('<scr' + 'ipt language="javascript" src="' + dynamicJSvariable + '"></scr' + 'ipt>';

</script>
</head>

the word "script" has to be split to avoid nested script tag weirdness even though it is in a string literal.

Clearline
07-18-2003, 09:03 AM
Glenngv
Your mother should be proud! Had to work with it a bit, but mainly to clean up my own syntax errors with the VAR,

Final code that works like a charm:
<HTML><HEAD><Title>Test: </Title>
<script type="text/javascript">

var useJS=top.JSuse; // JSuse declared in frameset

document.write('<scr'+'ipt language="javascript" src="'+useJS+'"></scr'+'ipt>');

</script>

I only modified the original code by taking out spaces, and sticking the ) at the end of the write( cmd -> ).

Thanks :thumbsup:

glenngv
07-18-2003, 09:35 AM
Welcome :)
Sorry for the typo. :D

Actually, there is a more elegant way to dynamically add js file. You need to use DOM to append script node in the head. But older browsers will not support it.

var myscript=document.createElement('script');
myscript.src=top.JSuse;
document.getElementsByTagName('head')[0].appendChild(myscript);

Clearline
07-18-2003, 08:00 PM
Thanks, I'll keep that code in mind for future use. I am trying to keep my designs compatable with IE 4 or greater, I don't even try to compensate for Netscape. I figure that the IE based browser engine is universal and free, and so anyone can stick it on their computers.

I would like to tell the PCs when the pages are opened to use IE, despite any other default browser, especially as all the pages are contained within a single window.

At first, I tried to avoid using js, but could not really accomplish what I am doing without it (except by having a lot of redundant htm files). I figure, that even if a person does not like to have JS run on their computer, unchecked; they can turn on Java for trusted sites, and mark the page base as trusted.

Thanks again