2011-04-14 153 views
20

我想打开一个弹出窗口并禁用父窗口。以下是我正在使用的代码;Javascript打开弹出窗口并禁用父窗口

function popup() 
{ 

    popupWindow = window.open('child_page.html','name','width=200,height=200'); 
    popupWindow.focus(); 
} 

出于某种原因,父窗口不会被禁用。我需要一些额外的代码或情况如何?

同样,我正在寻找类似于我们在使用showModalDialog()时得到的东西。它不允许选择父窗口在all..Only的事情是我想用做同样的事情window.open

也请提出,这将是跨浏览器兼容的代码..

+0

你是什么意思的“禁用父窗口”? – atlavis 2011-04-14 08:53:47

+0

你是什么意思desabled? – 2011-04-14 08:54:10

+0

禁用意味着不允许用户在父窗口中选择说出的文本..实际上不允许他选择父项上的任何内容。 – testndtv 2011-04-14 10:15:35

回答

28
var popupWindow=null; 

function popup() 
{ 
    popupWindow = window.open('child_page.html','name','width=200,height=200'); 
} 

function parent_disable() { 
if(popupWindow && !popupWindow.closed) 
popupWindow.focus(); 
} 

,然后在

<body onFocus="parent_disable();" onclick="parent_disable();"> 

正如你在这里要求的父窗口的body标签声明这些功能是父窗口的新

<html> 
<head> 
<script type="text/javascript"> 

var popupWindow=null; 

function child_open() 
{ 

popupWindow =window.open('new.jsp',"_blank","directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200"); 

} 
function parent_disable() { 
if(popupWindow && !popupWindow.closed) 
popupWindow.focus(); 
} 
</script> 
</head> 
<body onFocus="parent_disable();" onclick="parent_disable();"> 
    <a href="javascript:child_open()">Click me</a> 
</body>  
</html> 

内容的完整的HTML代码.jsp

<html> 
<body> 
I am child 
</body> 
</html> 
+0

我在IE中试过这个..但是它允许我选择或者玩父窗口。 – testndtv 2011-04-14 10:13:10

+0

你是什么意思,你可以选择父窗口,因为对我来说它工作正常。它不会允许你在父窗口上执行任何操作 – anu 2011-04-14 10:24:10

+0

对于我来说,我可以在孩子打开时对父母执行操作。 。你可以复制/粘贴你所拥有的complte HTML代码。 – testndtv 2011-04-14 10:56:51

2

要我的知识,你不能禁用浏览器窗口。

你可以做的是创建一个jQuery(或类似类型)弹出窗口,当这个弹出窗口出现时,你的父浏览器将被禁用。

在弹出窗口中打开您的子页面。

+0

我知道jQuery模态对话框..但我不能使用它。 .it实际上是同一个页面内的div .. – testndtv 2011-04-14 10:13:51

+0

@hmthur - 如果你不想让它在同一个页面中,那么你可以在该div中使用iframe,并给出你想要在src属性中看到的页面'iframe' – niksvp 2011-04-14 11:22:30

1

关键词是模态对话框。

因此,没有内置的模态对话框提供。

但是你可以使用许多其他的this

1

这就是我最终做到的!你可以在你的身体上放置一层(全尺寸)高Z指数,当然也是隐藏的。当窗口打开时,您将使其可见,使其侧重于单击父窗口(图层),并且当打开的窗口关闭或提交时,最终将消失。

 .layer 
    { 
     position: fixed; 
     opacity: 0.7; 
     left: 0px; 
     top: 0px; 
     width: 100%; 
     height: 100%; 
     z-index: 999999; 
     background-color: #BEBEBE; 
     display: none; 
     cursor: not-allowed; 
    } 

,并在主体层:

   <div class="layout" id="layout"></div> 

功能打开的弹出窗口:

var new_window; 
    function winOpen(){ 
     $(".layer").show(); 
     new_window=window.open(srcurl,'','height=750,width=700,left=300,top=200'); 
    } 

保持新窗口聚焦:

  $(document).ready(function(){ 
      $(".layout").click(function(e) { 
       new_window.focus(); 
      } 
     }); 

,并在打开窗口:

function submit(){ 
     var doc = window.opener.document, 
     doc.getElementById("layer").style.display="none"; 
     window.close(); 
    } 

    window.onbeforeunload = function(){ 
     var doc = window.opener.document; 
     doc.getElementById("layout").style.display="none"; 
    } 

我希望这将有助于:-)

0

喜的是@anu张贴的答案是正确的,但根据需要将其完全不会工作。通过稍微改变child_open()函数它工作正常。

<html> 
<head> 
<script type="text/javascript"> 

var popupWindow=null; 

function child_open() 
{ 
if(popupWindow && !popupWindow.closed) 
    popupWindow.focus(); 
else 
    popupWindow =window.open('new.jsp',"_blank","directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200"); 

} 
function parent_disable() { 
    if(popupWindow && !popupWindow.closed) 
    popupWindow.focus(); 
} 
</script> 
</head> 
    <body onFocus="parent_disable();" onclick="parent_disable();"> 
    <a href="javascript:child_open()">Click me</a> 
    </body>  
</html>