467,861 Members | 1,518 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Firefox: can't get document.write to work

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.
Jul 23 '05 #1
  • viewed: 2444
Share:
7 Replies
On Thu, 18 Nov 2004 11:56:47 +0100, Remi Bastide
<Du****************@irit.fr.dummy> wrote:

[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).
<HTML>
Valid documents should have a DOCTYPE declaration. See
<URL:http://www.w3.org/TR/html4/struct/global.html#h-7.2> for more
information.
<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:

<script type="text/javascript">
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
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.
Jul 23 '05 #3
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.
Jul 23 '05 #4
"Remi Bastide" <Du****************@irit.fr.dummy> wrote in message
news:sj********************************@4ax.com...
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
Jul 23 '05 #5
Remi Bastide <Du****************@irit.fr.dummy> wrote:
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.
Jul 23 '05 #6
Remi Bastide <Du****************@irit.fr.dummy> wrote in message news:<sj********************************@4ax.com>. ..
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:opener.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. :)
Jul 23 '05 #7
RobG wrote:
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:opener.newWindowHtml','new Window');
}
</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:opener.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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Keith Thornhill | last post: by
4 posts views Thread by Schraalhans Keukenmeester | last post: by
15 posts views Thread by Dan | last post: by
4 posts views Thread by lmarceglia | last post: by
6 posts views Thread by laramie.hartmann | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.