Firefox: can't get document.write to work

R

Remi Bastide

I'm trying to open a blank window and write a message in it.
The following page works as expected in IE, but in Firefox the message
is not written:

<HTML>
<HEAD>
<TITLE>Document.write bug</TITLE>
<Script Language="JavaScript">
function load() {
var w = window.open('','','');
w.document.open();

w.document.write('<html><body><h1>hello</h1></body></html>');
w.document.close();
}
</Script>
</HEAD>
<BODY>
<H1>Document.write bug</H1>
<a href="javascript:load()">Open Window</a>
</BODY>
</HTML>

I'm looking for the "cross-browser" way to achieve this effect.
Thanks for any hint.
 
M

Michael Winter

On Thu, 18 Nov 2004 11:56:47 +0100, Remi Bastide

[snip]

Please don't use tabs to format code when posting to Usenet. They usually
cause the text to wrap, making posts difficult to read. Instead, use
spaces (preferably two per level).

Valid documents should have a DOCTYPE declaration. See
<HEAD>
<TITLE>Document.write bug</TITLE>
<Script Language="JavaScript">

The language attribute has been deprecated for over six years. Use the
(required) type attribute instead:

function load() {
var w = window.open('','','');
w.document.open();
w.document.write('<html><body><h1>hello</h1></body></html>');

You should also make sure that you write valid HTML into a new document.
w.document.close();
}
[snip]

I'm looking for the "cross-browser" way to achieve this effect.

There's nothing IE-specific there, though I'd recommend your window.open
call included a name. I've seen problems when it's not included. Also,
don't specify a feature string if you're not specifying any features.

var w = window.open('', 'myWin');

The probable cause of this issue is that when you try to access the new
window and manipulate it, the browser hasn't finished preparing that
window.

One possible workaround is to initially open a document that signals when
it's loaded. Once that condition has been flagged, you can overwrite the
contents of the window.

Mike
 
F

Fred Oz

Michael Winter wrote:
[...]
The probable cause of this issue is that when you try to access the new
window and manipulate it, the browser hasn't finished preparing that
window.

One possible workaround is to initially open a document that signals
when it's loaded. Once that condition has been flagged, you can
overwrite the contents of the window.
[...]

This is for the OP, but I'll add it after Michael's excellent advice...

Another way that works is to open the window, then write your HTML into
an array, then write it to the window. This should introduce just
enough of a delay to let the window open.

When writing large amounts of HTML, this can be the fastest method of
writing to the window - even faster than a concatenated string:

<script type="text/javascript">
function load() {
var w = window.open('','newWindow');
w.document.open();

var a = [
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01',
' Transitional//EN" ',
'"http://www.w3.org/TR/html4/loose.dtd">',
'<html><head><title>No bugs</title>',
'</head><body>',
' <h1>It works</h1>',
'<p>Add lots more HTML here...</p>',
'</body></html>',
];

w.document.write(a.join(''));
w.document.close();
}
</script>

It is also much better for the link to do something useful rather than
just have JavaScript attached. If JS isn't working on the users
browser for some reason, they will click a link that just does
nothing.
Replace the href="javascript..." with a link to a useful page and put
the javascript into an onclick function:

<a href="aUsefulURL.html" onclick="
load();
return false;
">Open Window</a>

If JS isn't working, the user will be taken to aUsefulURL.html, if it
is working, the new window will open (if not blocked) and return false;
will prevent the browser from following the link.

Cheers, Rob.
 
R

RobG

Michael Winter wrote:
[...]
One possible workaround is to initially open a document that signals
when it's loaded. Once that condition has been flagged, you can
overwrite the contents of the window.

Mike
[...]

This is for the OP, but I'll add it after Michael's excellent advice...

Another way that works is to open the window, then write your HTML into
an array, then write it to the window. This should introduce just
enough of a delay to let the window open.

When writing large amounts of HTML, this can be the fastest method of
writing to the window - even faster than a concatenated string:

<script type="text/javascript">
function load() {
var w = window.open('','newWindow');
w.document.open();

var a = [
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01',
' Transitional//EN" ',
'"http://www.w3.org/TR/html4/loose.dtd">',
'<html><head><title>No bugs</title>',
'</head><body>',
' <h1>It works</h1>',
'<p>Add lots more HTML here...</p>',
'</body></html>',
];

w.document.write(a.join(''));
w.document.close();
}
</script>

It is also much better for the link to do something useful rather than
just have JavaScript attached. If JS isn't working on the users
browser for some reason, they will click a link that just does nothing.
Replace the href="javascript..." with a link to a useful page and put
the javascript into an onclick function:

<a href="aUsefulURL.html" onclick="
load();
return false;
">Open Window</a>

If JS isn't working, the user will be taken to aUsefulURL.html, if it
is working, the new window will open (if not blocked) and return false;
will prevent the browser from following the link.

Cheers, Rob.
 
M

McKirahan

Remi Bastide said:
I'm trying to open a blank window and write a message in it.
The following page works as expected in IE, but in Firefox the message
is not written:

<HTML>
<HEAD>
<TITLE>Document.write bug</TITLE>
<Script Language="JavaScript">
function load() {
var w = window.open('','','');
w.document.open();

w.document.write('<html><body><h1>hello</h1></body></html>');
w.document.close();
}
</Script>
</HEAD>
<BODY>
<H1>Document.write bug</H1>
<a href="javascript:load()">Open Window</a>
</BODY>
</HTML>

I'm looking for the "cross-browser" way to achieve this effect.
Thanks for any hint.

I just installed FireFox 1.0 and it worked for me.

I invoked your test page via: http://localhost/testpage.htm

as well as via: file:///C:\inetpub\wwwroot\testpage.htm
aka file:///C:%5Cinetpub%5Cwwwroot%5Ctestpage.htm
 
R

Remi Bastide

Thanks to all for the feedback.

I was using Firefox 1.0 Beta, after upgrading to 1.0 final, this
annoying behaviour appears to be gone.
 
R

RobB

Remi Bastide said:
I'm trying to open a blank window and write a message in it.
The following page works as expected in IE, but in Firefox the message
is not written:

<HTML>
<HEAD>
<TITLE>Document.write bug</TITLE>
<Script Language="JavaScript">
function load() {
var w = window.open('','','');
w.document.open();

w.document.write('<html><body><h1>hello</h1></body></html>');
w.document.close();
}
</Script>
</HEAD>
<BODY>
<H1>Document.write bug</H1>
<a href="javascript:load()">Open Window</a>
</BODY>
</HTML>

I'm looking for the "cross-browser" way to achieve this effect.
Thanks for any hint.


Not sure what you're referring to, working fine here. Here's a
superior (imo) alternative:

<HTML>
<HEAD>
<TITLE>Document.write bug</TITLE>
<Script Language="JavaScript">
function getHTML()
{
return '<html><body><h1>hello</h1></body></html>';
}
function load() {
var w = window.open('javascript:eek:pener.getHTML()','w','');
}
</Script>
</HEAD>
<BODY>
<H1>Document.write bug</H1>
<a href="javascript:void load()">Open Window</a>
</BODY>
</HTML>

Like a favelet. Just a tip: if you're just learning this stuff, don't
get in the habit of naming global functions 'load' or other DOM-ish
names. Sooner or later you'll run afoul of an already named property.
Choose names that are likely to be unique. :)
 
G

Grant Wagner

RobG said:
Another way that works is to open the window, then write your HTML into
an array, then write it to the window. This should introduce just
enough of a delay to let the window open.

When writing large amounts of HTML, this can be the fastest method of
writing to the window - even faster than a concatenated string:

<script type="text/javascript">
function load() {
var w = window.open('','newWindow');
w.document.open();

var a = [
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01',
' Transitional//EN" ',
'"http://www.w3.org/TR/html4/loose.dtd">',
'<html><head><title>No bugs</title>',
'</head><body>',
' <h1>It works</h1>',
'<p>Add lots more HTML here...</p>',
'</body></html>',
];

w.document.write(a.join(''));
w.document.close();
}
</script>

Instead of working on some undetermined delay to "pause things long enough",
why not do it the correct way:

<script type="text/javascript">
function load() {
window.newWindowHtml = [
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01',
' Transitional//EN" ',
'"http://www.w3.org/TR/html4/loose.dtd">',
'<html><head><title>No bugs</title>',
'</head><body>',
' <h1>It works</h1>',
'<p>Add lots more HTML here...</p>',
'</body></html>',
].join('\n');

var w = window.open('javascript:eek:pener.newWindowHtml','newWindow');
}
</script>

Alternatively:

function load() {
window.newWindowHtml = [
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01',
' Transitional//EN" ',
'"http://www.w3.org/TR/html4/loose.dtd">',
'<html>',
'<head>',
'<title>Loader</title>',
'</head>',
'<body onload="opener.callBack(window);">',
'</body>',
'</html>',
].join('\n');

var w = window.open('javascript:eek:pener.newWindowHtml', 'newWin');
}
function callBack(w) {
w.document.open();
w.document.write(...);
w.document.close();
}
</script>

The benefit of the second example is that the content output to the new
window is separated from the opening of the window.
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top