Forum Moderators: open
I have decided to learn JavaScript from the bottom up, having previously only done little more than cut 'n' paste.
I am at the point where I am learning about the object model, and have used the following code to open a new window and write some content to it.
<html>
<head>
<title>WRITING TO A NEW WINDOW</title><script language="JavaScript">
<!--
function smallWindow()
{
var myWindow;
myWindow = open("","weeWindow","height=100,width=100");
myWindow.document.open();
myWindow.document.write("<form><input type='button' value='close' onClick='window.close();'
/></form>");
myWindow.document.close();
}
//-->
</script>
</head>
<body onLoad="smallWindow();">
<p>Opening a new dynamic window and writing content to it</p>
</body>
</html>
The issue I have is this: I was told that the window object is the default object and does not need to be stated before every method. So how come when I type onClick='close();' it doesn't work?
And just out of interest, you will see that I have named the window "weeWindow" in the open statement. What is that name for?
window.close() should work. I think your problem may be more pedestrian. Try
onclick="window.close()" (case-sensitive). I would also recommend against doing a direct write, if possible. Instead, find an object (I usually use
<div> objects for this), and set its innerHTML property. Thusly: <body>
<div id="spaz"></div>
</body>
In order to write to "spaz", you do the following:
<script type="text/javascript">
document.getElementById('spaz').innerHTML = "This is now in 'spaz.'";
</script>
You can write the basic HTML framework to the window, but use DOM manipulation to populate it after that (You could also use DOM to create the entire window contents as well).
The W3Schools site [w3schools.com] has some great JavaScript [w3schools.com] and DOM [w3schools.com] stuff.
Hope this helps.
When a function fired by an event on any object calls the close method, the window.close method is implied.<SCRIPT LANGUAGE="JScript">
function foo() {
close();}
</SCRIPT>
<BODY onclick="foo();">
Click this page and window.close() is called.
</BODY>When an event on any object calls the close method, the document.close method is implied.
<BUTTON onclick="close();">
Click this button and document.close() is called.
</BUTTON>
In the 2nd case
<BUTTON onclick="close();">
is equiv to
buttonElement.onclick = function () { close(); }
So if "this" were document it may expain it, but "this" is buttonElement?
Perhaps its just some IE quirk?
myWindow = open("","weeWindow","height=100,width=100");.... What is that name for?
That is the id you are assigning to the window (url, id, parmeters.) You should make this unique. If you click the link, then click again, every click will open in the same window because it's calling it by the same name. An easy window id function that insures a new window actually opens every time is to name the window based on time:
var day = new Date();
var id = day.getTime();
var params = 'width=100,height=100';
var win = open('',id,params);
I don't know why close() won't work, maybe it has something to do with scope. As in, the onClick is trying to close the next opject from where it's located, and you can't close a button (?)
Out of curiosity, you could try this
<body onClick="close();">
then click somewhere in the document body <shrug>.
Just one of those things I've never given much though to. :-)