not sure why close window function not working

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,
 
B

bruce barker

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.
 
L

Lau Lei Cheong

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.
 
G

Guest

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.
 
G

Guest

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.
 
G

Guest

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.
 
K

Kevin Spencer

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.
 
G

Guest

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.
 
G

Guest

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.
 
K

Kevin Spencer

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.
 
K

Kevin Spencer

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.
 
G

Guest

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.
 
L

Lau Lei Cheong

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.
 
L

Lau Lei Cheong

Yes. So the workaround would be to supply
"javascript:fhandle=windo.open('url');". :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top