How the message will become the link of new window and how the window will be close by click on that link?
Write the following statement to close the window by click on link.
<A HREF="javascript:window.close()" >
Create the new window like this:
options = "toolbar=0, status=0,menubar=0,scrollbars=0,"
+"resizable=0,width=300,height=200";
newwindow=window.open("","mywindow", options);
newwindow.document.writeln(LocalTime);
newwindow.document.write(contents);
newwindow.document.close();
Example: Simple example to close the new window by click on link.
<HTML>
<HEAD><title>Link to close the window</title>
<SCRIPT LANGUAGE="JavaScript">
function MyWindow(message)
{
now = new Date();
LocalTime = now.toLocaleString();
contents='<body bgcolor="olive">'+'<h2>Hello</h2>'+
'Click on the link to close this window<br>'+
'<A HREF="javascript:window.close()" >'+ message +'</A>'
//Create new Window
options = "toolbar=0, status=0,menubar=0,scrollbars=0,"
+"resizable=0,width=300,height=200";
newwindow=window.open("","mywindow", options);
newwindow.document.writeln(LocalTime);
newwindow.document.write(contents);
newwindow.document.close();
}
</SCRIPT>
</HEAD>
<BODY bgcolor="#ffffcc" onLoad="this.form1.text1.focus()">
<FORM NAME="form1">
<TABLE BORDER=1 bgcolor="beige">
<tr><td>Please Enter the message or text:<br><br />
<INPUT NAME="text1" TYPE=Text SIZE="50" MAXLENGTH="50"><br><br />
This message will become a link to close the new window.<br /><br />
<INPUT TYPE=Button VALUE="Create new window" onClick="MyWindow(form.text1.value)">
</td></tr>
</TABLE>
</FORM>
</BODY>
</HTML>
Output:
Figure 1: Output of the script.
When you write the message in the box and click on the button then message will become the link of the new window (see the figure2).
Figure 2: Message display as a link on the new window.
If you click on the link then new window will be close.