0

hi all,
i have one question about variable in JavaScript

<HEAD>
<TITLE>Window Opener and Closer</TITLE>
<SCRIPT LANGUAGE="JavaScript">
var newWindow
function makeNewWindow() {
newWindow = window.open("","","HEIGHT=300,WIDTH=300")
}
function closeNewWindow() {
if (newWindow) {
newWindow.close()
newWindow = null
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT TYPE="button" VALUE="Create New Window" onClick="makeNewWindow()">
<INPUT TYPE="button" VALUE="Close New Window" onClick="closeNewWindow()">
</FORM>
</BODY>
</HTML>

can anyone explain why here newWindow variable equals to null.
what does mean here null

newWindow=null

Thanks in advance

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by azegurb
0

why here newWindow variable equals to null.

That is a way of preventing closeNewWindow() from trying to close an already closed window.
Once the window has been closed [and newWindow has been set to null] the test if (newWindow) at the beginning of that function will not be true.

0

but you know without it the function also works
with that variable or without it. what difference does it make in this function
thanks for attention

0

Possibly the author was afraid that the browser would throw an error if .close() were invoked on an already closed window.

Maybe some of them do.

Or possibly other code is expected to test if (newWindow) to prevent trying to access elements within that window when it doesn't exist, or to control the logic flow.

0

Thanks very much. i also have idea. may be it wastes additional system resources
Thanks Thanks Thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.