admin管理员组文章数量:1287849
How can i close all the IE browser window in asp,
I am opening many windows..from javascript by window.open()... I need to close all the windows by clicking the button in main page(parent window).
some times we have open in in c# it self
btnShow.Attributes.Add("onclick", @"var windowVar=window.open('" + sAppPath + @"', 'parent');windowVar.focus(); return false;");
at the time how can i put in array in javascript.
How can i do it?
How can i close all the IE browser window in asp,
I am opening many windows..from javascript by window.open()... I need to close all the windows by clicking the button in main page(parent window).
some times we have open in in c# it self
btnShow.Attributes.Add("onclick", @"var windowVar=window.open('" + sAppPath + @"', 'parent');windowVar.focus(); return false;");
at the time how can i put in array in javascript.
How can i do it?
Share Improve this question edited Jan 10, 2010 at 22:23 Jan Jongboom 27.3k9 gold badges84 silver badges121 bronze badges asked Dec 16, 2009 at 6:15 subramanisubramani 1,0695 gold badges13 silver badges22 bronze badges3 Answers
Reset to default 10Concept
Whenever you open a window from the main page, keep a reference to the opened window (pushing it onto an array works well). When the main page button is clicked, close each referenced window.
Client Script
This JavaScript is for the main page. This works for an HTML or ASPX page.
var arrWindowRefs = [];
//... assume many references are added to this array - as each window is open...
//Close them all by calling this function.
function CloseSpawnedWindows() {
for (var idx in arrWindowRefs)
arrWindowRefs[idx].close();
}
Opening a window and pushing it onto the above array looks something like this:
// Spawn a child window and keep its reference.
var handle = window.open('about:blank');
arrWindowRefs.push(handle);
Microsoft's JavaScript window.open(..) method and its arguments are outlined here.
Different browsers might have variations or proprietary ways to keep references to opened windows or to enumerate through them, but this pure JavaScript way is very patible with browsers.
Button
Finally the HTML Input button to initiate the above code would be
<input type="button"
name="btn1" id="btn1" value="Click Me To Close All Windows"
onclick="CloseSpawnedWindows()">
If it's an ASP.NET Button Control then call JavaScript this way
<asp:Button ID="Button1" runat="server" Text="Click Me To Close All Windows"
OnClientClick="CloseSpawnedWindows()" />
Troubleshooting ASP.NET client script (PostBack and AJAX fix)
If your aspx page posts back to the server, the client code will be destroyed and lose it's array with child window references (and those windows will remain open). If this is a concern, you might want to use AJAX for partial page refreshes, to prevent the entire page and its scripts from being destroyed.
(Shown using Framework 3.5 samples)
For ASP.NET AJAX you'll be using something like a ScriptManager instance to enable partial page refresh inside UpdatePanel controls (lots of samples).
<%@Page... %>
<asp:ScriptManager EnablePartialRendering="True" /> Enable AJAX.
<script>
// PUT JAVASCRIPT OUT HERE SOMEWHERE.
// Notice the client script here is outside the UpdatePanel controls,
// to prevent script from being destroyed by AJAX panel refresh.
</script>
<asp:UpdatePanel ID="area1" runat="server" ... > ... </asp:UpdatePanel>
<asp:UpdatePanel ID="area2" runat="server" ... > ... </asp:UpdatePanel>
etc...
A lot more detail about ASP.NET AJAX can be provided but this is just a start in case you need it.
Remember, in the case of AJAX, don't refresh the part of the page containing the above Client Script because you want it to persist the array through server callbacks.
You just need to keep references to the opened windows, then you can close them at any time by calling their close()
method, e.g.
<script>
var myWindow = window.open('http://www.google./');
var myWindow2 = window.open('http://www.bing./');
function closeAll()
{
myWindow.close();
myWindow2.close();
}
</script>
<input type="button" onclick="closeAll();" value="Close all" />
This probably isn't possible. You need to get a JS message to the windows you are trying to close.
referenceToWindowObject.close()
Since you are trying to do this from the main window, you can't write the JS directly to the opened window.
Since you are trying to do this from ASP.NET, you are presumably loading a new page in the original window, so (presumably) any handles you might have kept (the return value from the call to window.open
) will be lost since it is a new page.
The only way you could achieve this is if you were to keep the handles around by using (for example) frames.
本文标签: aspnetHow can i close all the IE browser windows that I opened in JavaScriptStack Overflow
版权声明:本文标题:asp.net - How can i close all the IE browser windows that I opened in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741322460a2372283.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论