window.open在启动URL后返回null

时间:2014-06-17 15:54:49

标签: javascript jquery html

我试图限制用户为我的应用程序打开多个IFrame窗口。我的代码如下,

<html>
<head>
<title>Test for Window Already Open</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>

var gmyWin=null;

function myOpenWindow(winURL,winName,winFeatures,winObj)
{
  var theWin;
  theWin = window.open(winURL,winName,winFeatures);
  // here theWin variable is null
  return theWin;
}
</script>
</head>

<body>
<h1>Click this link to open the new window:</h1>
<p><a href='javascript:;' onClick='javascript:gmyWin=myOpenWindow("http://example:7001/tester/helloworld.jsp","myWin","height=630,width=1100",gmyWin);return false'>open new window</a> </p>
</body>

我尝试访问Win变量,但它总是返回null。我想检查窗口是否打开但是我无法检查,window.open在打开URL后返回null。 请帮我解决问题。

2 个答案:

答案 0 :(得分:0)

只需设置一个标志。

function myOpenWindow(winURL,winName,winFeatures,winObj)
{
  var windowIsOpen = false;
  window.open(winURL,winName,winFeatures);
  windowIsOpen = true;  

  return windowIsOpen;
}

答案 1 :(得分:0)

很好地获取变量的值,你可以返回一个布尔变量

<html>
<head>
<title>Test for Window Already Open</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function myOpenWindow(winURL,winName,winFeatures,winObj)
{
  var windowOpen = false;
  window.open(winURL,winName,winFeatures);
  windowOpen = true;  

  return windowOpen;
}
</script>
</head>

<body>
<h1>Click this link to open the new window:</h1>
<p><a href='javascript:;' onClick='javascript:gmyWin= return myOpenWindow("http://example:7001/tester/helloworld.jsp","myWin","height=630,width=1100",gmyWin);return false'>open new window</a> </p>
</body>

你必须为布尔值

添加一个return语句