Click to See Complete Forum and Search --> : problems putting javascript in a new window


tupu
10-16-2003, 04:05 AM
I apologize for my english, I'll try to explain what is the problem corretly.

Ok, I'm opening a second window (B) from a first one (A). Then, also from A, I put code in the second window B. I write html and javascript code.
What's the problem? Javascript function isn't working. Let's see the code (simplified):

WINDOW A
--------

<html>
<...>
<script>
function fullwin() {
newWindow = window.open("",...)
newWindow.document.write(...)
newWindow.document.write(writing a script)
newWindow.document.write(writing a form)
</script>
<...>
</html>

WINDOW B
--------

Let's see the final content of window B:

<html>
<...>
<script>
function verify() {
var themessage = "";
if (document.form.log1.value=="") {themessage = "FILL THE FORM!";}
if (themessage == "") {document.form.submit();}
else {alert(themessage);return false;}}
</script>
<...>
<form action="anywhere" method=post)
<input type=text name=log1>
<input type=button value=SEND onclick="verify();"'>
</form>
<...>
</html>

I need to open a new window from A, and then write the code also from A because this is the only way a banner doesn't appear.
Well, when I get window B, and I press the button, nothing happens. I saw the code writed in the document and is correct (or I think so).
What's happening, please? Maybe the function is not loaded for the browser because I have writed from another window?
Thanx.

lillu
10-16-2003, 04:50 AM
If you include javascript within document.write, make sure you escape any characters that would make the browser parse the content incorrectly.
This puts the whole content of a page in the variable str, then you call it from the new window and the function executes.

<html>
<head>
<title>window opener</title>
<script language="javascript">
var str = "Hello, world!";
newWin = window.open('file.htm', 'winfile', 'height=250, width=250', false);
newWin.document.write('<html><head><scr' + 'ipt> var str = "Hello, world!";
function writeHTML() { window.alert(str); }<\/scr' + 'ipt> ');
newWin.document.write('<html><body> ' + str + '<br \/> How are you? ');
newWin.document.write('<form><input type="button" value="Save" onClick="writeHTML()"> ');
newWin.document.write('<\/form><\/body><\/html> ');
</script>
</head>
<body>
<script language="javascript">
document.write(str);
</script>
</body>
</html>