Forum Moderators: open
In this and some other forums I frequent, I've seen where individuals want to specify which script file to load as the page is loading. While that isn't really possible, a possible way around it occurs to me.
At first wash the idea didn't seem too desireable. Upon reflection, however, I realized that one might be able to dispense with some of the bloated, labyrnthine code often seen with implementations designed to support multiple browser standards.
If the original document contains an iframe, hidden or otherwise, the first document's onload event could trigger a function that would do the browser determinations, then use document.write() to load the appropriate .js file into the iframe. Then its functions could be called with dot notation and the frames[0] nonsense, or such.
I don't have time to try this before going to work, but I thought I'd throw it out for your consideration.
Oh, drat! It occurs to me now that those silly older browsers probably don't support iframe. Oh, well.
This is a very widespread technique, and I was wondering whether Rambo's suggestion was motivated by having experienced complications using it.
BTW. I've had reasonable success writing in script tags, not with innerHTML, but with createElement. This is very useful for extending bookmarklets. It's probably not so good for our current topic, as ..er.. it's not always supported.
Initially, I was looking for a universal (back to v. 4) solution. As my appended comment lamenting the lack of iframe support in "older browsers" suggests, I did not succeed.
[edited by: Rambo_Tribble at 4:18 pm (utc) on June 18, 2004]
document.write("<scri" + "pt ....")
or some such.
There is also some other very useful (even critical) nugget to use...but I can't remember where I put it.
I remembered that nugget I was on about, but it's...
a) Not something we'd probably need.
b) Almost (but not quite) self-evident.
Say we have our process writing scripts into the doc, each one - amongst other things - has a different, browser-specific version of a function, orgyPorgy
<script>
if(Browser=="Alpha")
document.write("<scr.... etc
else if(Browser=="Beta")
document.write("<scr.... etcorgyPorgy() // error: this won't work
</script>
<script>
orgyPorgy() // this does work
</script>
The function call must be inside a new script block. I think this is because the written script element actually appears below the block that holds the statements that write it...or something.