简体   繁体   中英

function Window.focus doesn't work in IE (8, 9, 10, 11)

Here is my code, and this code to open new tab and focus on it.

  • If tab is not opened yet, I will open it with specify name.
  • If tab is opened, I will focus on it by using window.focus();

Quick test: http://jsfiddle.net/u2jd2oLw/1/

Javascript Code

var tabs = [];
$('#btnOpenTab').on('click', function(){
    openTab('http://google.com.vn', 'GG');
});


function openTab(url, tabNm) {
    try {
        if (tabs[tabNm]) {
            tabs[tabNm].focus();
        } else {
            var mTab = window.open(url, tabNm);
            tabs[tabNm] = mTab;
            tabs[tabNm].focus();
        }
    } catch(e) {
        alert(e.message);
    }
}

HTML Code

<button id="btnOpenTab">Open Tab</button>

Actually, I check in IE11, it occurred that " Access is denied "

And I tried to turn off option: "Turn on Pop-up Blocker" by un-check CheckBox.

Do you have any suggestion?

Thanks!!

This will work once due to the security issues

$(function() {
  $('#btnOpenTab').on('click', function(){
    openTab('http://google.com.vn', 'GG');
  });
});

function openTab(url, tabNm) {
  var mTab = window.open("", tabNm);
  mTab.document.write('<body onload="window.focus(); location.replace(\''+url+'\')">Please wait...</body>');
  mTab.document.close();
}

It failed to run twice because once the page loads it is no longer same origin.

I tried with an iFrame, but Google blocks that too now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM