-->

Check if pop-up blocker is enbled when open new ta

2019-04-20 10:47发布

问题:

I want to open new window in new process/context in chrome, (Im not sure if it possible with window.open but with the following example its working ) currently if it was regular window you can check with the following example and to see if the pop-up blocker is enabled

ar newWin = window.open(url);             

if(!newWin || newWin.closed || typeof newWin.closed=='undefined') 
{ 
     //POPUP BLOCKED
}

but I want to open the new window in new process without window.open like following

var prod = document.getElementById("myElement"); 
var aTag = document.createElement('a');
aTag.setAttribute('href',"http://cnn.com");
//THIS TWO LINES do the job
aTag.setAttribute('rel',"noreferrer");
aTag.setAttribute('target',"_blank");
prod.appendChild(aTag);
aTag.click();
prod.removeChild(aTag);

used with this reference: http://news.softpedia.com/news/Force-Google-Chrome-to-Open-Links-in-New-Processes-128962.shtml

from the post to open new tab in new context you should use:

  aTag.setAttribute('rel',"noreferrer");
  aTag.setAttribute('target',"_blank");

While this is working, sometimes the new window/tab is blocked with the pop-up blocker,I just want to know this and inform the user internally that the new window is blocked and please enable it ...which option do I have?

My requirements are this:

  1. Open window in new process/context
  2. if the popup blocker is blocked notify the user

How it can be done ?

UPDATE

I need it since when you click to open new window from existing window and the second window is opened and you return to the first/source window and you want to do something it's blocked!

To simulate this you can create this simple file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BlockTAB</title>
</head>

<br/>
<br/>
<button onclick="windowOpen('http://cnn.com')">window open native</button>
<br/>
<br/>
<button onclick="windowOpen('http://google.com')">sample</button>

<script>
    function windowOpen(url){
        window.open(url);
    }

</script>
</html>

Now do the folloiwng

  1. Run the program (the html file), this open the CNN tab
  2. inspect the CNN tab and put break point on it (in the source tab you can find the javascript, put it in any place you choose),until it stops(you need to refresh the page until you see the debugger stops...
  3. Now go back to the first tab were the two buttons is and you see that it is blocked, you cannot do anything like click and etc...

How to handle open the new tab without blocking the first/source tab?

UPDATE 2

There is a way to simulate the pop-up blocker if it's not happen with the code with

aTag.setAttribute('rel',"noreferrer"); aTag.setAttribute('target',"_blank");

add this following code the previous example

<script src="https://cdn.jsdelivr.net/bluebird/3.4.5/bluebird.js"></script>
<br/>
<br/>
<button onclick="EnabledPPB('http://cnn.com')">Blocker</button>

function delayFN(url) {
    Promise.delay(500)
        .then(function() {
            var newWin = window.open(url);
        })
}

function EnabledPPB(url) {
    Promise.delay(100)
        .then(function() {
            delayFN(url);
        })

}

回答1:

Not very sure do you want to make sure to open a new window or new process, so maybe I will answer both.

If you literally means you want to make sure Chrome starts a new process, there is no way to do that in Javascript, not even the code you shown with window.open. However, as Chrome is opening a new process for every tab, it is safe to assume your message is in a new process as long as your user is using Chrome. Checking what browser your user is using, on the other hand, is possible. Although your user may still fake the user agent (i.e. the browser), it should be quite enough for internal use.

More reference on Chrome using new process instead of new thread for each tab.

If you want to make sure your user is opening your message in a new window, the one with window.open is the only option. You may trigger window.open by adding event listener or simply use the onclick attribute in HTML. There is no way to do with HTML alone, and there should be no reason to search for answer when you have already found a way to do that with javascript?