Link to home
Start Free TrialLog in
Avatar of strider031598
strider031598

asked on

Botton

I was using the following code to close javascript windows, but I later found out it was only working with Navigator and not IE:

<form name="close">
<center><input type="button" value="Close" onCLick="window.close();
</form>

The code makes an HTML button that SHOULD close a JS window, but it doesn't work with Microsoft's browser.  Is there something I can do to make it compatible with both browsers?
Avatar of oubelkas
oubelkas

Well first of all, close the code properly :

<form name="close">
<center><input type="button" value="Close" onClick="window.close();"></center>
</form>
 
Then, it should work in both IE and NS. Use the name of the opened window to close it ( e.g. myWindow.close(); )

the close() method is compatible in both IE and NS

Joseph
Avatar of strider031598

ASKER

I tried your suggestion, but the button still doesn't close the window when I use IE! :(
Try this :

blabla.html

<HTML>
<HEAD>
<script language="javascript">
<!--
function window2Up(){
  window2=window.open("win2.html", "win2","height=200, width=200");
}
// -->
</script>
</head>


<body>
<form>
<input type="button" value="open window 2" onClick="window2Up()">
<input type="button" value="close window 2" onClick="window2.close()">
</form>
</body>
</html>

win2.html

<html>
<body>
window 2
</body>
</html>

Now open the window with the button, and then close it...it works...

Joseph

Avatar of Michel Plungjan
Joseph, you mean "use the window handle" not the name
in "Use the name of the opened window to close it ( e.g. myWindow.close(); ) "

Michel
Sorry, Michel,

you're right, mixed up....it should be the window handle, thus like window2 in the last comment I gave. The name is defined with the properties (like "win2").

Joseph
i was having a look at this and the first script should work, in fact it does if you add <Script Language = "JavaScript"></Script>

to the head, you don't need to put any code in their but it needs to be there.

So if you change your origional script ot:

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
</SCRIPT>

</HEAD>
<BODY>

<P>&nbsp;</P>
<form name="close">
<center>
<input type="button" value="Close" onClick="window.close()">
</form>

</BODY>
</HTML>


It should work and it will give the msg that it is trying to close itself.  Has anyone else found this that if you don't add the script tags even though they aren't needed, the script doesn't run??

Hmmm - If you have VB scripts on the same page I have seen this.

The correct syntax to use is this then:

<input type="button" value="Close" language="javascript" onCLick="window.close();">

Michel
nice
I tried all suggestions and the one that worked for me was Ginger Ed's. So GE, if you want the points, just repost as answer. :)
Ginger is welcome to it, but didn't mine work?????????

Michel
Or mine??
ASKER CERTIFIED SOLUTION
Avatar of Ginger_Ed
Ginger_Ed

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Guys, I honestly tried all of your suggestions, and no doubt that Michel's LOOKED like the best, but it didn't work.  The only one that worked was Ginger Ed's. Anyway, thanks to all of you.
No seriously, I have no problem with the points, only the fact that I seemed to be misinformed.

I have found the actual problem

change form name=close to
name=closeit
and you will be just fine.

the problem is you used a reserved word for the form name.

Michel
Michel, your last suggestion turned out to be right.  I'm wondering, though, if close is a reserved word, then how come it worked when I added the lines

<Script Language = "JavaScript"></Script>

above the code?  Hmm...weird.
One never knows ;-)

Possibly for the same reason that I thought language=javascript would work - that IE defaults the scripting engine to JScript instead of VBScript.

Michel
Well, as far as  I know, I never add <Script Language = "JavaScript"></Script>

when using the close() method. Indeed, hmmm....weird. Ah well.

Joseph
Joseph, IE was only reacting to having the form named close and then a function executing window.close

since IE scopes the variables in a "userfriendly" way, you can sometimes access variables of the document via the window object
so naming the form close, overwrote the close method of the window object.

window.close=null;
would do the same to the button...

Michel
Oh, yes, of course. Mmm, so the best thing here to say to strider is,

"It's best not to use reserved words as names for objects"

Yep, yep...

Joseph