not sure why close window function not working

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi I have 2 functions in java script, one opens a second window-this works,
the other is supposed to close this second window, does not seem to be
working. Just wondering if anyone had any ideas.
Here is the code,
the functions are
<script language="javascript">
function openwin(){
win_usr=window.open ("control_numinfo.aspx")
}
function closewin(){
win_usr.close();
}
</script>
below is how I am trying to call the functions,
<script language="javascript" event="onclick()" for="btn_user">
openwin();
</script>
<script language="javascript" event="onclick()" for="btn_clear">
closewin();
</script>
</form>
thanks,
 
the code is ok, but if the open button click fires a postback, or a postback
is done before you call close it will not work, as you will have lost the
window handle.

-- bruce (sqlwork.com)

| Hi I have 2 functions in java script, one opens a second window-this
works,
| the other is supposed to close this second window, does not seem to be
| working. Just wondering if anyone had any ideas.
| Here is the code,
| the functions are
| <script language="javascript">
| function openwin(){
| win_usr=window.open ("control_numinfo.aspx")
| }
| function closewin(){
| win_usr.close();
| }
| </script>
| below is how I am trying to call the functions,
| <script language="javascript" event="onclick()" for="btn_user">
| openwin();
| </script>
| <script language="javascript" event="onclick()" for="btn_clear">
| closewin();
| </script>
| </form>
| thanks,
| --
| Paul G
| Software engineer.
 
I've written sample code and tested it in IE6, and it works.
<html>
<head>
<script language="javascript">
function openwin(){
win_usr=window.open ("control_numinfo.aspx")
}
function closewin(){
win_usr.close();
}
</script>
<script language="javascript" event="onclick()" for="btn_user">
openwin();
</script>
<script language="javascript" event="onclick()" for="btn_clear">
closewin();
</script>
</head>
<body>
<form runat=server>
<input id="btn_user" type="button">
<input id="btn_clear" type="button">
</form>
</body>
</html>

However it's not going to be work in Mozilla based browsers such as Mozilla,
Netscape and Firefox.
For reference on how to do it, see here:
http://wdvl.com/Authoring/JavaScript/Events/capturing_ns.html

Also, make sure your button do NOT have "runat = 'server'" attribute.
 
ok thanks for the information

Lau Lei Cheong said:
I've written sample code and tested it in IE6, and it works.
<html>
<head>
<script language="javascript">
function openwin(){
win_usr=window.open ("control_numinfo.aspx")
}
function closewin(){
win_usr.close();
}
</script>
<script language="javascript" event="onclick()" for="btn_user">
openwin();
</script>
<script language="javascript" event="onclick()" for="btn_clear">
closewin();
</script>
</head>
<body>
<form runat=server>
<input id="btn_user" type="button">
<input id="btn_clear" type="button">
</form>
</body>
</html>

However it's not going to be work in Mozilla based browsers such as Mozilla,
Netscape and Firefox.
For reference on how to do it, see here:
http://wdvl.com/Authoring/JavaScript/Events/capturing_ns.html

Also, make sure your button do NOT have "runat = 'server'" attribute.
 
ok thanks for the information, could be that a postback is happening so
loosing the handle. Guess one could save the handle in a session
variable,not quite sure how this would work.
 
Hi I tried the code and it worked but when I use .NET to add the buttons and
take out the run at server attribute the button does not even show up on the
form.
See below

<asp:Button id="btn_user" style="Z-INDEX: 101; LEFT: 152px; POSITION:
absolute; TOP: 80px" Text="Open" CausesValidation="False"></asp:Button>
Just wondering if there is another attribute I should add in place of the
runat server.
Lau Lei Cheong said:
I've written sample code and tested it in IE6, and it works.
<html>
<head>
<script language="javascript">
function openwin(){
win_usr=window.open ("control_numinfo.aspx")
}
function closewin(){
win_usr.close();
}
</script>
<script language="javascript" event="onclick()" for="btn_user">
openwin();
</script>
<script language="javascript" event="onclick()" for="btn_clear">
closewin();
</script>
</head>
<body>
<form runat=server>
<input id="btn_user" type="button">
<input id="btn_clear" type="button">
</form>
</body>
</html>

However it's not going to be work in Mozilla based browsers such as Mozilla,
Netscape and Firefox.
For reference on how to do it, see here:
http://wdvl.com/Authoring/JavaScript/Events/capturing_ns.html

Also, make sure your button do NOT have "runat = 'server'" attribute.
 
A PostBack is certainly going to lose the handle. You realize that each Page
instance is occurring in a vacuum, don't you? When the page reloads into the
browser, everything is new.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
Hi thanks for the response. Yes everything is new as you mentioned. I was
able to get it working by using the <input statement for the button that
opens the child window, does not cause a post back. I have another window
that has a column of buttons in a data grid and when these buttons are
selected, it opens a child window. Guess in this case it is posting back to
the server so will have to some how save off the handle name and then
retreive it and use it in the onunload event of the parent window in the body
tag, so when the parent window navigates away the child window if left open
will be closed.
 
Just had an additional question, below is what I have that opens a child
window(created by .NET for a hyperlink data column grid),
is there a way to assign a handle to this so I can build a close function
and call it when the parent window unloads?
<asp:HyperLinkColumn Text="View" Target="_blank"
DataNavigateUrlField="Data_Item_ID"
DataNavigateUrlFormatString="child.aspx?i_Data_Item_ID={0}"

thanks Paul.
 
Afraid not, Paul. The problem isn't the name, it's what it points to, which
is gone after the PostBack. It's an actual in-memory handle to a browser
window. There is simply no way to persist it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
Hi Paul,

Only the JavaScript window.open() method returns a handle to a window.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
ok thanks for the information.

Kevin Spencer said:
Afraid not, Paul. The problem isn't the name, it's what it points to, which
is gone after the PostBack. It's an actual in-memory handle to a browser
window. There is simply no way to persist it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
It is because ASP.NET buttons are "always" run at server.
If they're not placed inside <form runat="server">, you'll see a parser
error tell you to do so.
Also, using <asp:> controls will introduce one more postback to "create" the
control it'll cause slower performance, that's why people here trend to
advise others use plain HTML controls instead.

In this case you should use <input type="button/submit/image"> instead.

Paul said:
Hi I tried the code and it worked but when I use .NET to add the buttons and
take out the run at server attribute the button does not even show up on the
form.
See below

<asp:Button id="btn_user" style="Z-INDEX: 101; LEFT: 152px; POSITION:
absolute; TOP: 80px" Text="Open" CausesValidation="False"></asp:Button>
Just wondering if there is another attribute I should add in place of the
runat server.
 
Back
Top