Javascript打开弹出窗口并禁用父窗口 [英] Javascript to open popup window and disable parent window
问题描述
我想打开一个弹出窗口并禁用父窗口。以下是我正在使用的代码;
I want to open a popup window and disable the parent window. Below is the code that I am using;
function popup()
{
popupWindow = window.open('child_page.html','name','width=200,height=200');
popupWindow.focus();
}
由于某种原因,父窗口不会被禁用。我需要一些额外的代码或者是什么情况?
For some reason, the parent window does not get disabled. Do I need some additional code OR what is the case?
同样,我正在寻找类似于我们使用showModalDialog()时得到的东西。它根本不允许选择父窗口。只是我想要使用window.open完成相同的事情。
Again, I am looking for something similar to what we get when we use showModalDialog()..i.e. it does not allow to select parent window at all..Only thing is I want to get the same thing done using window.open
另外请建议将使用的代码跨浏览器兼容..
Also please suggest the code which will be cross-browser compatible..
推荐答案
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标签中声明这些函数
and then declare these functions in the body tag of parent window
<body onFocus="parent_disable();" onclick="parent_disable();">
正如您所要求的那样是父窗口的完整html代码
As you requested here is the complete html code of the parent window
<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>
以下new.jsp的内容
Content of new.jsp below
<html>
<body>
I am child
</body>
</html>
这篇关于Javascript打开弹出窗口并禁用父窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!