472,791 Members | 1,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

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
7 2684
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Keith Thornhill | last post by:
I'm trying to calculate the top and left distance from the side of the browser of an object (either absolutely or relatively positioned) and i'm running into a weird problem as i'm trying to make...
6
by: Cliff R. | last post by:
Hi, I use a handy little Javascript Flash detection script on a number of sites (copied below). Usually works great, but I just started trying Firefox and it's not working. A few browsers are...
4
by: Schraalhans Keukenmeester | last post by:
I have no clue why below code (found it somewhere and altered it a wee bit to my needs) will run without problem in both IE and Mozilla FireFox 1.0 but in the latter it takes up close to 100% cpu....
3
by: pantagruel | last post by:
The following does not work in firefox: <script defer="defer"> var x=document.getElementsByName("repositoryNamespace") alert(x.length + " elements!")
15
by: Dan | last post by:
Hi. I've got the following line of code which works fine in IE ... line_1_numbers = document.getElementsByTagName ('table').rows (0).cells (0).innerText; But it Firefox, it barks saying: ...
4
by: lmarceglia | last post by:
Hi, I have this website that doesn't work in Firefox 1.5: www.pianetaluca.com The HTML source is: <TITLE>PianetaLuca</TITLE> </HEAD>
4
by: evgenyg | last post by:
Hello ! We have the following situation - when Ajax request is sent what's being returned by the server is usually an XML (which is used for DOM updates) but sometimes it's HTML which is a whole...
6
by: laramie.hartmann | last post by:
I have a script (see below) that accesses a XML file and displays the contents through a series of document.write calls. This all works fine in IE, but not at all in Firefox. I get no errors in the...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.