Creating Script on the Fly

R

Russ

Hi All,
I have a problem getting the following simple example
of "document.write" creating a script on the fly
to work in all html browsers.
It works in I.E., Firefox, and Netscape 7 above.
It doesn't seem to work in Netscape 4. Am I missing
something with it? When I look at page source in
Netscape 4 the script isn't even shown.
Can Netscape 4 create scripts on the fly at all?

thanx
Russ Kinter

<HTML>
<HEAD>
<script language="javascript">
function loadBrowser() {
var aStr = '<HTML>';
aStr += '<HTML>';
aStr += '<HEAD>';
aStr += '<SCRIPT language ="JavaScript">';
aStr += 'function vrmlHTML(){ ';
aStr += 'resizeTo(400,300);';
aStr += 'moveTo(0,0); }';
aStr += '<\/SCRIPT>';
aStr += ' <\/HEAD>';
aStr += ' <BODY onload="vrmlHTML()">';
aStr += '<\/BODY >';
aStr += '<\/HTML>';
document.open();
document.write(aStr);
document.close();
}
</SCRIPT>
</HEAD>
<BODY onload="loadBrowser()">
</BODY>
</HTML>
 
M

Martin Honnen

Russ wrote:

I have a problem getting the following simple example
of "document.write" creating a script on the fly
to work in all html browsers.
It doesn't seem to work in Netscape 4. Am I missing
something with it? When I look at page source in
Netscape 4 the script isn't even shown.
Can Netscape 4 create scripts on the fly at all?

Netscape 4 problem in 2005? As far as I remember it there are indeed
problems to document.write HTML with script in Netscape 4 and have the
script being parsed and available in the document.written page.

function loadBrowser() {
var aStr = '<HTML>';
aStr += '<HTML>';

Why two said:
aStr += '<HEAD>';
aStr += '<SCRIPT language ="JavaScript">';
aStr += 'function vrmlHTML(){ ';
aStr += 'resizeTo(400,300);';
aStr += 'moveTo(0,0); }';
aStr += '<\/SCRIPT>';

I am not sure I remember any safe strategy to get the script recognized
but things you could tried are
- try whether external script is loaded e.g.
<script src="file.js" type="text/javascript"></script>
- I think in some cases people successfully worked around the problem
by inserting some dummy script first (which Netscape 4 then ignored)
but which caused further scripts to be recognized so you could try
to document.write at least two <script> elements
 
R

RobG

Russ said:
Hi All,
I have a problem getting the following simple example
of "document.write" creating a script on the fly
to work in all html browsers.
It works in I.E., Firefox, and Netscape 7 above.
It doesn't seem to work in Netscape 4. Am I missing
something with it? When I look at page source in
Netscape 4 the script isn't even shown.
Can Netscape 4 create scripts on the fly at all?

thanx
Russ Kinter

<HTML>
<HEAD>
<script language="javascript">
function loadBrowser() {
var aStr = '<HTML>';
aStr += '<HTML>';
aStr += '<HEAD>';
aStr += '<SCRIPT language ="JavaScript">';
aStr += 'function vrmlHTML(){ ';
aStr += 'resizeTo(400,300);';
aStr += 'moveTo(0,0); }';
aStr += '<\/SCRIPT>';
aStr += ' <\/HEAD>';
aStr += ' <BODY onload="vrmlHTML()">';
aStr += '<\/BODY >';
aStr += '<\/HTML>';
document.open();
document.write(aStr);
document.close();
}

It doesn't help your problem, but you may find it faster for larger
chunks of HTML to use an array:

var aStr = [
'<HTML><HEAD>',
'<SCRIPT type="text/JavaScript">',
'function vrmlHTML(){ ',
'resizeTo(400,300);',
'moveTo(0,0); }',
'<\/SCRIPT>',
' <\/HEAD>',
' <BODY onload="vrmlHTML()">',
'<\/BODY >',
'<\/HTML>'
];
document.open();
document.write( aStr.join('') );
document.close();


Or you can just use a single document.write:

document.open();
document.write(
'<HTML><HEAD>',
'<SCRIPT type="text/JavaScript">',
'function vrmlHTML(){ ',
'resizeTo(400,300);',
'moveTo(0,0); }',
'<\/SCRIPT>',
' <\/HEAD>',
' <BODY onload="vrmlHTML()">',
'<\/BODY >',
'<\/HTML>'
);
document.close();


Both the above are a lot faster than string concatenation and are
easier to code (to me at least).
 
V

VK

Russ said:
Hi All,
I have a problem getting the following simple example
of "document.write" creating a script on the fly
to work in all html browsers.
It works in I.E., Firefox, and Netscape 7 above.
It doesn't seem to work in Netscape 4. Am I missing
something with it? When I look at page source in
Netscape 4 the script isn't even shown.
Can Netscape 4 create scripts on the fly at all?

As DevEdge went to hell knows where, you have to accept my weak memory
:)
NN4 do not parse (so doesn't execute) JavaScript inserted into another
page or layer ising document.write()

The work around would be to put your script into myScript.js file. NN4
also stops parsing as soon as it sees '<script>' string in write()
statement. But you can cheat it rather easily by doing:

document.write('<scr');
document.write('ipt src="myScript.js">');
document.write('</scr');
document.write('ipt>');

The other known bug in NN4 was that any language indications were
ignored if an external script file was used. So don't bother with
"language" or "type".

Wht do you need this antique anyway?
 
X

X l e c t r i c

RobG wrote:

"
Or you can just use a single document.write:

document.open();
document.write(
'<HTML><HEAD>',
'<SCRIPT type="text/JavaScript">',
'function vrmlHTML(){ ',
'resizeTo(400,300);',
'moveTo(0,0); }',
'<\/SCRIPT>',
'<\/HEAD>',
'<BODY onload="vrmlHTML()">',
'<\/BODY >',
'<\/HTML>'
;
document.close();
"

Hi Rob,

It's me again with more questions.

The single document.write you suggested would have been my solution
also, except I would have put the entire content in single quotes.
Because that's all I know.

My questions are why do you have it divided up into groups within single
quotes followed by commas, and how do you determine what each group will
be within the single quotes ?

Later, Art.
 
R

RobG

X said:
RobG wrote:

"
Or you can just use a single document.write: [...]

Hi Rob,

It's me again with more questions.

The single document.write you suggested would have been my solution
also, except I would have put the entire content in single quotes.
Because that's all I know.

My questions are why do you have it divided up into groups within single
quotes followed by commas, and how do you determine what each group will
be within the single quotes ?

It can be one long string, but it's easier to read and maintain if you
use blocks just as if you were writing it as plain HTML or whatever.

Write the source, test it, insert quotes and commas using find/replace,
then copy & paste it into the document.write() statement.
 
R

Randy Webb

VK said the following on 7/31/2005 8:37 AM:
As DevEdge went to hell knows where, you have to accept my weak memory
:)
NN4 do not parse (so doesn't execute) JavaScript inserted into another
page or layer ising document.write()

Actually, it did. You could dynamically load a script, or external
script file, by opening a layer, write to it, and close the layer.
The work around would be to put your script into myScript.js file. NN4
also stops parsing as soon as it sees '<script>' string in write()
statement. But you can cheat it rather easily by doing:

document.write('<scr');
document.write('ipt src="myScript.js">');
document.write('</scr');
document.write('ipt>');

I have never seen, nor do I remember, that ever being a problem. But all
The other known bug in NN4 was that any language indications were
ignored if an external script file was used. So don't bother with
"language" or "type".

That's not good advice to give. Always use the type attribute and let
the browser deal with it how it wants. Do not produce invalid code just
because one ancient browser ignores it.
 
X

X l e c t r i c

RobG wrote:

"It can be one long string, but it's easier to read and maintain if you
use blocks just as if you were writing it as plain HTML or whatever.

Write the source, test it, insert quotes and commas using find/replace,
then copy & paste it into the document.write() statement."

Hi Rob,

Thanks for the explanation. It makes good sense to me. I have a couple
of pages where I put html code to a page with document.write. I'll go
back and arrange it this way. Great idea.

Later, Art.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top