468,140 Members | 1,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,140 developers. It's quick & easy.

Creating Script on the Fly

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>

Jul 31 '05 #1
7 1898


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 <HTML>?
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

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 31 '05 #2
Russ wrote:
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).

--
Rob
Jul 31 '05 #3
VK

Russ wrote:
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?

Jul 31 '05 #4
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.

Aug 1 '05 #5
X l e c t r i c wrote:
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.

--
Rob
Aug 1 '05 #6
VK said the following on 7/31/2005 8:37 AM:
Russ wrote:
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()


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
it would take is an escape in the <\/script> tag to cure that problem.
Why make it harder than it has to be?
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 1 '05 #7
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.

Aug 1 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Craig Jurney | last post: by
13 posts views Thread by jkimbler | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.