IE allows the ID attribute on the script tag, as does NS6/7
NN4 wont load the .js file.
So, I do this:
if (document.layers)
{
document.write('<script type="text/javascript" src="jsFile1.js"><\/script>')
}else{
document.write('<script type="text/javascript" src="jsFile1.js"
id="myScriptTag"><\/script>')
}
//jsFile1.js has nothing but an alert in it to show me it was loaded
//NN4.80, IE6.0, NS6.1, NS7, Moz1.0.1 and Opera6/7 all give me that alert
function changeJSFile()
{
if (document.all && document.getElementById)
{
document.getElementById('myScriptTag').src='jsFile1-2.js';
//IE allows the src to be changed via the id.src
//nothing else allows it, that I can find.
}
else
{
if (document.getElementById)
{
//This works in NS6. It doesn't work in NS7 or Mozilla
document.getElementById('scriptDiv').innerHTML = '<script
type="text/javascript" src="jsFile1-2.js"><\/script>';
}
}
if (document.layers)
{
document.layers['scriptDiv'].document.open();
document.layers['scriptDiv'].document.write('<script type="text/javascript"
src="jsFile1-2.js"><\/script>Hmm');
document.layers['scriptDiv'].document.close();
}
}
With this HTML:
<div id="scriptDiv" style="position: absolute"></div>
It works, as written, in NN4.80, IE6.0, and NS6. Anybody have any ideas on how
to make it work in NS7 or Mozilla? All on PC. Any MAC testing would be greatly
appreciated. Also, testing in IE4/5/5.5 would be greatly appreciated.
If its a totally different way that doesnt work in NS6, is there a method that
I can try to detect to tell me if its NS7 or Mozilla without resorting to
userAgent?
Nothing I have tried in Opera6 or 7 will allow me to dynamically load it. And
stay within the document. If I can do it in an IFrame or a hidden frame, I may
have too. But, trying to stay away from it if possible.
--
Randy
All code posted is dependent upon the viewing browser
supporting the methods called, and Javascript being enabled.
Testing loading an additional script file from a running page had patchy
results. IE 4 would not load a new JS file with any method that I tried,
Opera 5.12, 6.04 & 6.05 also would not (no real surprise there) and I
could not get Konqueror 2.1.2 to do it (but more recent versions might
be able to).
The most generally successful approach was to use document.createElement
to create a new script node using code like:-
var s = document.createElement('script');
s.src = fileName; //the name of the JS file.
document.documentElement.appendChild(s);
- which worked on Netscape 6.2.3 & 7.0, Mozilla 0.9, 1.0 & 1.2, Phoenix
0.4, K-Meleon 0.7 (I would assume all other Gecko browsers), IE 5.0 &
6.0. But _not_ any version of Opera 7. (all Win 98 or NT 4)
Modifying the innerHTML property of a DIV, using code like:-
scriptDiv.innerHTML = '<SCRIPT type="text/javascript" src="'+
fileName+'"><\/SCRIPT>';
- worked with Mozilla 0.9 and Netscape 6.2.3 and (strangely) Opera 7.0b1
(the first beta release) but _not_ Opera 7.02 (the current release
version). (Win 98)
Changing the SRC attribute of an existing script element, using:-
scriptEl.setAttribute('src', fileName);
- or -
scriptEl.src = fileName;
- worked only with IE 5.0 and 6.0 but IE 4 generated a 'cannot set
property at run time' error with the second version. (all Win 98 or NT
4)
The Netscape 4.n Layer re-writing version worked OK on Netscape 4.51 &
4.79 on Win98 and 4.77 on Linux.
I might get a chance to do some MAC testing in a week or two (if I
remember).
It looks like the createElement has broadest support, it is a pity that
Opera 7 is not interested. Falling back to changing the innerHTML for
just the Beta release of Opera 7 does not strike me as satisfactory and
re-setting the SRC is not needed as IE 5.0 and 6.0 are covered by the
createElement version. Unfortunately, I would think that the IFRAME
approach might actually be the most cross-browser method.
Richard.
<--snip lots of useful information-->
>It looks like the createElement has broadest support, it is a pity that
>Opera 7 is not interested. Falling back to changing the innerHTML for
>just the Beta release of Opera 7 does not strike me as satisfactory and
>re-setting the SRC is not needed as IE 5.0 and 6.0 are covered by the
>createElement version. Unfortunately, I would think that the IFRAME
>approach might actually be the most cross-browser method.
>
>Richard.
>
Many thanks Richard. When I wrote the original, I had IE5.0, NN4.76 (I
believe), NS6 had just come out, and Opera 5 was the "latest" (IIRC). It was
about 3 years or so ago and since it worked in those 3 (NN, IE and NS), I was
happy with it. Now, I am back to it trying to make it as cross-browser as I can
without resorting to the IFrame solution (the whole purpose of the dynamic .js
was to kill a frameset).
Hi!
Well, I personally use always the same method, even if I don't test it
with a lot of browsers, but with IE 5+, NN 4.7x, NN 6, Mozilla 1.0,
Opera 6 works...
If you want you can try this with all your own browsers, maybe can
work! :)
Here is the simple `solution' I use:
In the place where you need to load the js file, put this little
script:
<SCRIPT type="text/javascript" language="javascript">
<!--
document.write('<SCRIPT type="text/javascript" language="javascript"
src="yourjs.js"></'+'SCRIPT>');
//-->
</SCRIPT>
Note the [...]</'+'SCRIPT>' expression is to avoid confusion with the
other closing tag below!
You can see this stuff in action on my website.
Follow this link:
http://edoardoontheweb.interfree.it/html/en/cfi.html
then click on the `Search' button in the `Italian Commune/Foreign
state of birth' section of the form;
look at the popup source!
I'll appreciate if you try this page with other browsers and you post
here your comments... :)
Thanx
bye
--
Edoardo
----------------------------------------------
.::EdoardoOnTheWeb::.
http://edoardoontheweb.interfree.it/index.html
mailto:edoardo...@interfree.it
----------------------------------------------