Forum Moderators: open

Message Too Old, No Replies

Script Augmentation Surgery

firming flabby scripts the iframe way

         

Rambo Tribble

3:51 pm on Jun 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, tee hee hee! (Sorry, this is my first post starting a thread and I'm just giddy about it!)

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.

DrDoc

4:45 pm on Jun 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Iframes are IE4+ and NN6+
Don't remember when Opera started supporting iframes. But they have for a while.

Rambo Tribble

2:07 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In checking my reference tomes, it appears that iframes, part of the HTML 4 spec, were first supported in IE 3 and NS 6, in the respective lineages. Unfortunately, that leaves out NS 4, though I suppose it might be possible to apply the same principle through a layer with a src value and, if desired, a visibility="hide".

Bernard Marx

8:36 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(You may not be surprised but) I don't understand.
Why can't this process - of selective doc.write() - be applied to the main page from the start?

Rambo Tribble

1:20 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It could, but with the inline equivalent of several large .js files you would be transmitting a lot of unnecessary baggage. My idea was simply to break up the code threads into more manageable chuncks, from both a maintenance and transmission point of view.

Bernard Marx

1:50 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I got that bit. You are saying that scripts could be loaded selectively into the iframe (or layer for NS4) using
write()
. I'm wondering why the same thing can't be done on the page itself instead.

DrDoc

2:56 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because once the page has loaded, document.write() clears the contents...

Rambo Tribble

3:24 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



. . . and because simply writing (as with innerHTML) to the document after the fact won't force the reading of the external file.

Bernard Marx

4:00 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I'm getting there now. An iframe is used because doc.write() can be used at any time (esp. after loading). That makes sense, but can't we write the script tag into the main document whilst the page is loading?

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.

Rambo Tribble

4:09 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, yours may be a very salient suggestion, Bernard, for newer browsers. It doesn't address the NS 4 issue, however, as it doesn't support createElement.

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]

Bernard Marx

4:12 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



..which, of course, defeats the object of the exercise! So..

Have you had trouble just writing script tags into the document as it loads?

Rambo Tribble

4:20 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, but it is not a technique I've actually employed outside of just messing around. I really have no idea if it would force the loading of the .js file, though one might think it would.

Bernard Marx

4:32 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Same here, but I do see it a lot. Usually banner advertizing for free-hosting sites. I know there is some problem that requires the tag be written in pieces:

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.

Rambo Tribble

4:40 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The '<scr'+~ is because some browsers get very confused at intact script tags included in a document.write command. (Am I there? Am I there?) Kind of like too much Soma, if you know what I mean.

Bernard Marx

5:26 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you have a point (2 in fact).

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.... etc

orgyPorgy() // 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.