Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bgcolor of parent window?

Status
Not open for further replies.

monion03

Programmer
Apr 24, 2007
7
GR
hello. I'm a new javascript programmer and I need you help! I wrote the following code:

<HTML>
<HEAD>
<TITLE> A2P3 </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
var w=null;
function openWindow(){
var options="resizable=1,toolbar=1,scrollbars=1,menubar=0,status=0,location=0,directories=0, width=300,height=300";
w=window.open("","NewWindow",options);
makepage="<HTML><title> New Window </title>";
makepage +="<BODY><form><center><h1> New Window </h1>";
makepage +="<hr size = '2' width = '100%' color = 'gray' /><br/>";
makepage +="<input type='button' value='Green Color to the parent window' onClick='opener.document.bgcolor='green';' /><br/><br/>";
makepage +="<hr size = '2' width = '100%' color = 'gray' /><br/>";
makepage +="<input type='button' value='Close' onClick='window.close();' /><br/>";
makepage +="</form></center></body></html>";
w.document.write(makepage);

}

function changeColor(){
if(w==null){
window.alert("child window is not opened! Do you want to open it?");
openWindow();
}
w.document.bgColor="red";
}

function writeText(){
text=window.document.form1.NewText.value;
w.document.writeln(text);
}

function changeParent() {
opener.document.bgcolor = "green";
window.focus();
}

//-->
</SCRIPT>

</HEAD>

<BODY>
<form name="form1">
<input type="button" value="Open new window" onClick="openWindow();" /><br/><br/>
<input type="button" value="Red background color to the new window" onClick="changeColor();" /><br/><br/>
<input type="button" value="Text to the new window" onClick="writeText();" />
<input type="text" name = "NewText" size = "30" />
</form>
</BODY>
</HTML>

I use two windows. The child one is called w. The problem is that the first button of the child window, which must change the background color of the parent one is not working at all! Can you help me find what's wrong?

Thanks a lot
 
Hi

Change this line :
Code:
  makepage +="<input type='button' value='Green Color to the parent window' onClick='opener.document[red].body.style[/red].b[red]ack[/red]g[red]roundC[/red]olor=[red]\"[/red]green[red]\"[/red];' /><br/><br/>";

Feherke.
 
It's beause you have nested single quotes:

Code:
makepage +="<input type='button' value='Green Color to the parent window' onClick='opener.document.bgcolor=[!]'[/!]green[!]'[/!];' /><br/><br/>";

I'd rewrite the line as:

Code:
makepage +='<input type="button" value="Green Color to the parent window" onclick="opener.document.bgcolor=\'green\';" /><br/><br/>';

Hope this helps,
Dan






Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks a lot, I've been looking for a solution for hours! But can you explain me why the former code does not work properly?
 
Dan already told you - it's because you've nested your single quotes - and therefore it terminates the string early:

Code:
makepage +="<input type='button' value='Green Color to the parent window' onClick=[!]'opener.document.bgcolor='[/!]green';' /><br/><br/>";

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Thanks a lot. I have visited your site and it is extremelly interesting!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top