Forum Moderators: open

Message Too Old, No Replies

Small issue understanding window object

         

palgrave

8:02 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



Hi folks,

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?

cmarshall

11:58 am on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can have multiple windows (as in framesets). However, it is usually top of the local food chain, and can be referenced directly. In the example you show, I think that
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.

daveVk

12:56 pm on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I dont understand the logic of it but here is quote from msdn


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?

rocknbil

9:04 pm on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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. :-)