最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - IE11-facing issue while calling focus() on window object - Stack Overflow

programmeradmin2浏览0评论

Trying to run a following simple code on IE11 browser:

<!DOCTYPE html>
<html>

<head>
  <title>Popup Example</title>
  <script>
function ButtonClick2() {
var thewin = window.open("",'thewin','width=400, height=420,status=no');            
window.thewin.focus();

        }

  </script>
</head>

<body>
   <button onclick="ButtonClick2()">Click Me!</button>
</body>

</html>

ISSUE:On IE11 it gives the error statement "Unable to get property 'focus' of undefined or null reference"

Trying to run a following simple code on IE11 browser:

<!DOCTYPE html>
<html>

<head>
  <title>Popup Example</title>
  <script>
function ButtonClick2() {
var thewin = window.open("http://www.google.",'thewin','width=400, height=420,status=no');            
window.thewin.focus();

        }

  </script>
</head>

<body>
   <button onclick="ButtonClick2()">Click Me!</button>
</body>

</html>

ISSUE:On IE11 it gives the error statement "Unable to get property 'focus' of undefined or null reference"

Share Improve this question edited Jul 7, 2014 at 8:01 Teemu 23.4k7 gold badges59 silver badges111 bronze badges asked Jul 7, 2014 at 7:47 JITSU83JITSU83 691 gold badge3 silver badges10 bronze badges 2
  • 1 Seems to work at jsFiddle. Have you pop-up blocker on in IE? – Teemu Commented Jul 7, 2014 at 7:58
  • No, It work fine with IE8 but it gives issue with IE11. – JITSU83 Commented Jul 7, 2014 at 8:09
Add a ment  | 

2 Answers 2

Reset to default 3

This answer's late, but I thought that I'd post it just in case somebody came across this question in the future.

According to the answer here: https://stackoverflow./a/7025648/1600090, and my own experience, one possible cause could be that you're trying to open a window in a different internet zone for which Protected Mode is enabled. By default, IE11 enables Protected Mode for the Internet and Restricted zones but disables it for Local Intranet and Trusted Sites. So, for example, if your page (and/or site) are running in your Local Intranet zone and you're trying to open a new window in the Internet zone, window.open is going to return a null reference. If the page/site which is launching the new window is in the Internet zone, in my experience, window.open will return a reference. So, @ssut's example in jsfiddle is going to work because jsfiddle. and google. are probably both in the same zone (I'm assuming the Internet zone).

Please check the variable scope. This issue is not browser's problem.

In your code, var thewin = window.open(.. in the ButtonClick2 function, but window.thewin.focus(); is point to window object's thewin variable.

Change the code to thewin.focus(); then it works perfectly.

New code:

PE html>
<html>

<head>
  <title>Popup Example</title>
  <script>
function ButtonClick2() {
var thewin = window.open("http://www.google.",'thewin','width=400, height=420,status=no');            
thewin.focus();

        }

  </script>
</head>

<body>
   <button onclick="ButtonClick2()">Click Me!</button>
</body>

</html>
发布评论

评论列表(0)

  1. 暂无评论