write content for new window

J

Jenny

In the code below, I can write html content var t='<body
BGCOLOR=blue>'
for a new window. But if it contains javascript, such as var
t='<body onload="window.open('new1.html')">', this code will not work.
Could you confirm this and find such syntax rule for me on the
internet? Thank a lot.

<html><head>
</head><body>
<SCRIPT language=JavaScript>
w=open("new.html")
//var t='<body onload="window.open('new1.html')">'
var t='<body BGCOLOR=blue>'
w.document.write(t);
</SCRIPT ></body>
</html>
 
L

Lee

Jenny said:
In the code below, I can write html content var t='<body
BGCOLOR=blue>'
for a new window. But if it contains javascript, such as var
t='<body onload="window.open('new1.html')">', this code will not work.

The problem isn't that the string contains Javascript, but that the
syntax is wrong. You cannot nest quotes like that.
The correct syntax is:

t='<body onload="window.open(\'new1.html\')">'
 
G

Grant Wagner

Lee said:
Jenny said:

The problem isn't that the string contains Javascript, but that the
syntax is wrong. You cannot nest quotes like that.
The correct syntax is:

t='<body onload="window.open(\'new1.html\')">'

And it still won't work on many, many browsers, among them:

- IE < 6.0.2900 running the Google toolbar (in it's default configuration)
or another popup blocker
- IE 6.0.2900 (XP SP2) in it's default configuration
- Any Gecko-based browser in it's default configuration
- Opera 7.54 if configured to "Block unwanted (or all) pop-ups"

None of these browsers in the configurations shown allow a body onload
event to open a new window.

If this is an Intranet where you control the browsers being used, and their
configuration, then by all means open the new window in the onload event of
the body.

If this is a public Internet site, please abandon this design now.

And lastly, I believe document.write()ing a <body> tag into an HTML
document results in an invalid document:

<body>
<script type="text/javascript">
document.write('<body onload="window.open(...);">');
</script>
<!-- ... -->
</body>

- results in the document having two <body> tags, which is invalid HTML.

</head>
<script type="text/javascript">
document.write('<body onload="window.open(...);">');
</script>
<!-- ... -->
</body>

- results in the document having <script> tags outside of either
<head></head> or <body></body>, which is invalid HTML. You also run the
risk that JavaScript will be unavailable on the client and you'll end up
with no <body> tag at all, which would be invalid HTML.

If you want to set the onload event programmatically use:

<head>
<script type="text/javascript">
window.onload = function() {
window.open(...);
}
</script>
</head>
<body>
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top