始终保持打开同一个子窗口以及关闭父窗口同时自动关闭所有子窗口

需求如题,

原始需求来自今天CSDN asp.net 板块的一个求助贴:

None.gif 1 .点击一个可以打开新窗体的链接,如何实现如果窗体已打开,则将焦点转到已打开的窗体,否则打开新窗体。难点:如何判断窗体已打开,及将将打开的窗体Active?
None.gif
None.gif
2 .如何实现一个主窗体关闭时,将所有 打开的其他相关窗体一起关闭?
None.gif
None.gif
None.gif我们主管以前是做WinForm的,现在升级系统Asp.net做开发。但思维模式还是WinForm。提的这两个要求实难下手去做,请教各位是否有办法实现,若有请告之,谢谢!
None.gif
None.gifhttp://community.csdn.net/Expert/topic/
5587 / 5587936 .xml?temp = .7338983

虽然是有点变态,还硬是将他基本实现了,希望不会有太多潜在bug~

其实自己很早都有这种需求,只是之前都懒得去尝试,变样实现了,
至于父子窗口交互控制、互相调用方法、互相传值,地球人倒是实现了不少,

google + baidu 好像都没有完整的实例可供参考,只有自己实现

这里,我采取了 window.open ,还没有尝试 window.showModelessDialog,
对于 IE,来说后者可能更适合,更好交互控制

实现要点
1.  window.open 会返回新打开窗口的 window 对象
2. 实现一个模拟的简单 HashMap 存储子窗口的 window 对象
3. 每次 open 的时候,检索此 HashMap,确定子窗口是否已存在,
4. 若存在则直接切换焦点 (window.focus)
5. 若不存在,则 open 一个
6. 对于4,有可能子窗口已关闭,故采取了点技巧,先调用其 focus (其实可以任意方法),若出错,则也open 一个
7. 关闭 parent 的时候,遍历 HashMap,尝试关闭所有子窗口
8. 所有操作在父窗口实现
9. 整个实现原理其实很简单,只要需要熟悉js和dhtml,然后注意细节问题处理

目前   IE 6 sp1 测试通过,FF 由于不支持 window.focus 故不适合使用

对于 FF window.focus 问题参考:
http://hi.baidu.com/aolaii/blog/item/1bce87d6d9a1f62f06088b41.html
http://groups.google.com/group/mozilla.support.firefox/browse_thread/thread/470462a7876dd154/eae3d04ce24aa58e?lnk=st&q=focus+firefox+raise&rnum=1#eae3d04ce24aa58e

现在有点崇拜那句话了,"nothing is impossible!",当然前提是要合理的^_^

欢迎拍砖~


ContractedBlock.gif ExpandedBlockStart.gif
None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head>
None.gif    
<title>Parent window</title>
ExpandedBlockStart.gifContractedBlock.gif    
<script type="text/javascript">dot.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function openWin() dot.gif{
InBlock.gif        
//debugger;
InBlock.gif
        var sltWins = document.getElementById("sltWins");
InBlock.gif        
var url = sltWins.value;
InBlock.gif        
var winName = url.replace('.', '_');
InBlock.gif        
var win;
InBlock.gif        win 
= winMap[winName];
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            win.focus();            
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(e) dot.gif{
InBlock.gif            
// alert(e.message);
InBlock.gif
            // we need to open a new window when the child window has not 
InBlock.gif
            // been opened or the child window has been close.
InBlock.gif
            // as to the later, you also can implements some method that notices the parent window
InBlock.gif
            // to remove the child window from our winMap object when it is closing.
InBlock.gif
            // but it's a piece of hard work, i think so, because you must 
InBlock.gif
            // add the notice codes to all the child windows
InBlock.gif
            //             
InBlock.gif
            win = window.open(url, winName, "top=100,left=100,width=400,height=300"); 
InBlock.gif            winMap[winName] 
= win;
InBlock.gif            
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if(!win) dot.gif
InBlock.gif                alert(
"Sorry, fail to open the window.Some unexpected error occurs."); 
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
else dot.gif{
InBlock.gif                    
// i try to bind a callback function to the child window's unload event
InBlock.gif
                    // unfortunately, it seems not to work.
InBlock.gif//
                win.onunload = function() { 
InBlock.gif//
                    try {
InBlock.gif//
                        alert(opener.winMap[winName]);
InBlock.gif//
                        opener.winMap[winName] = null; 
InBlock.gif//
                        alert(opener.winMap[winName]);
InBlock.gif//
                    } catch(e) {
InBlock.gif//
                        // alert(e.message);
InBlock.gif//
                    }
InBlock.gif//
                };
InBlock.gif
                win.focus();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    
</script>
ExpandedBlockStart.gifContractedBlock.gif    
<script type="text/javascript">dot.gif
InBlock.gif    
// stores the opened window object
InBlock.gif
    var winMap = new Object();
InBlock.gif    
//winMap["TestedKey"] = "TestedValue";
ExpandedSubBlockStart.gifContractedSubBlock.gif
    window.onunload = function() dot.gif{
InBlock.gif        
// try to close all child windows.
ExpandedSubBlockStart.gifContractedSubBlock.gif
        for(var propName in winMap) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{                
InBlock.gif                winMap[propName].close(); 
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
catch(e) dot.gif{
InBlock.gif                
//alert(e.message);
ExpandedSubBlockEnd.gif
            }
           
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    
</script>
None.gif
</head>
None.gif
<body>
None.gif    
<h3>Maintaining the Parent window and Multi Child windows as in WinForm</h3>  
None.gif    
<h4>funcions:</h4>  
None.gif    
<ol>
None.gif        
<li>Open the same child window once.</li>
None.gif        
<li>Close all the child windows when the parent window is closing.</li>    
None.gif    
</ol>
None.gif    Child window:
None.gif    
<select id="sltWins">
None.gif        
<option selected="selected" value="c1.htm">#1</option>
None.gif        
<option value="c2.htm">#2</option>
None.gif    
</select>
None.gif    
<br />
None.gif    
<input id="btnOpen" type="button" value="Open the selected window" onclick="openWin()" />
None.gif
None.gif
</body>
None.gif
</html>
None.gif

下载

转载于:https://www.cnblogs.com/Jinglecat/archive/2007/06/08/776000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值