Two ways of opening a new window

A

Amir

What's the difference between the two ways of opening a new window and
writing to it?

Let's say I prepare a code that goes into a new window and save it in a
variable winHTML:

var winHTML = "<HTML><HEAD><TITLE>Any title</TITLE><BODY>Any
text</BODY></HEAD>";

Now, I have two ways to open a new window and write that code into the new
window:

One way is:
var newWin=window.open();
newWin.document.open();
newWin.document.write(winHTML);
newWin.document.close();
newWin.focus();

Other way is:
window.open("javascript:eek:pener.winHTML");

Is there any reason why to prefer one over the other?
 
A

Amir

Andrew said:
..perhaps you should validate that as HTML first.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

var winHTML = "<HTML><HEAD><TITLE>Any title</TITLE><BODY>Any
text</BODY></HTML>";

However, the JavaScript question was as clear as it is now. I wouldn't
attach too much importance to minor HTML errors, unless I present a question
that requires running a code.
 
L

Lee

Amir said:
What's the difference between the two ways of opening a new window and
writing to it?

Let's say I prepare a code that goes into a new window and save it in a
variable winHTML:

var winHTML = "<HTML><HEAD><TITLE>Any title</TITLE><BODY>Any
text</BODY></HEAD>";

Now, I have two ways to open a new window and write that code into the new
window:

One way is:
var newWin=window.open();
newWin.document.open();
newWin.document.write(winHTML);
newWin.document.close();
newWin.focus();

Other way is:
window.open("javascript:eek:pener.winHTML");

Is there any reason why to prefer one over the other?

I routinely use the second. The window.open() method returns
without guaranteeing that the window has been created yet, so
there have been many posts to this newsgroup asking why that
method fails some of the time.

I've never had any trouble with the second form.
That sort of thing is exactly what the "javascript:"
pseudo-protocol was created for.

note that you can send focus to the window (as you do in your
first example), with:

window.open("javascript:eek:pener.winHTML").focus();
 
T

Thomas 'PointedEars' Lahn

Amir said:
Andrew said:
..perhaps you should validate that as HTML first.
[...]

var winHTML = "<HTML><HEAD><TITLE>Any title</TITLE><BODY>Any
text</BODY></HTML>";

Nothing has changed, this is still invalid HTML. Besides, the ETAGO
delimiters (</) can cause trouble as it marks the end of the element in
SGML. If you use that sequence in scripts embedded in HTML documents,
escape it. In J(ava)Script you would do that with "<\/".
However, the JavaScript question was as clear as it is now. I wouldn't
attach too much importance to minor HTML errors, unless I present a
question that requires running a code.

You can't build a house on a swamp.

And please, trim your quotes and use a "From" header
that complies with Internet/Usenet standards.


PointedEars
 

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

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top