开发者

How can i close all the IE browser windows that I opened in JavaScript

How can i close all the IE browser window in asp.net,

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?


Concept

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 compatible 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.com/');
var myWindow2 = window.open('http://www.bing.com/');

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜