加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何关闭我在JavaScript中打开的所有IE浏览器窗口

发布时间:2020-12-16 09:14:35 所属栏目:asp.Net 来源:网络整理
导读:如何关闭asp.net中的所有IE浏览器窗口, 我打开很多windows..from javascript by window.open()… 我需要通过单击主页面(父窗口)中的按钮来关闭所有窗口. 有时我们在c#中打开它自己 btnShow.Attributes.Add("onclick",@"var windowVar=window.open('" + sAppP
如何关闭asp.net中的所有IE浏览器窗口,

我打开很多windows..from javascript by window.open()…
我需要通过单击主页面(父窗口)中的按钮来关闭所有窗口.

有时我们在c#中打开它自己

btnShow.Attributes.Add("onclick",@"var windowVar=window.open('" + sAppPath + @"','parent');windowVar.focus(); return false;");

那时我怎么能在javascript中放入数组.

我该怎么做?

解决方法

概念

每当您从主页面打开一个窗口时,请保持对打开的窗口的引用(将其推入阵列时效果很好).单击主页面按钮时,关闭每个引用的窗口.

客户端脚本

此JavaScript用于主页面.这适用于HTML或ASPX页面.

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();
}

打开一个窗口并将其推入上面的数组看起来像这样:

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

不同的浏览器可能有变体或专有方法来保持对打开的窗口的引用或通过它们进行枚举,但这种纯JavaScript方式与浏览器非常兼容.

按键

最后,用于启动上述代码的HTML Input按钮将是

<input type="button" 
    name="btn1" id="btn1" value="Click Me To Close All Windows"
    onclick="CloseSpawnedWindows()">

如果它是一个ASP.NET Button Control,那么以这种方式调用JavaScript

<asp:Button ID="Button1" runat="server" Text="Click Me To Close All Windows" 
        OnClientClick="CloseSpawnedWindows()" />

ASP.NET客户端脚本疑难解答(PostBack和AJAX修复)

如果您的aspx页面回发到服务器,客户端代码将被销毁并丢失它的子窗口引用数组(并且这些窗口将保持打开状态).如果这是一个问题,您可能希望使用AJAX进行部分页面刷新,以防止整个页面及其脚本被销毁.

(使用Framework 3.5示例显示)

对于ASP.NET AJAX,你将在UpdatePanel controls (lots of samples)中使用0700实例到enable partial page refresh.

<%@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...

可以提供有关ASP.NET AJAX的更多详细信息,但这只是您需要它的开始.

请记住,在AJAX的情况下,不要刷新包含上述客户端脚本的页面部分,因为您希望它通过服务器回调来保持阵列.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读