Check if pop-up blocker is enbled when open new tab
Asked Answered
G

1

11

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);
        })

}
Globin answered 28/9, 2016 at 12:50 Comment(13)
What does the ETag have to do with this? And yes, artificially triggered link clicks can be caught by the popup blocker as well – which is a good thing, because otherwise the douchevertisers would use this technique, too.Interflow
@CBroe - sorry my mistake this is not related to Etag...There is a way somehow to know that ?i just want to know that from my code that I can tell for the user to disable it, this is internal application ...\Globin
@CBroe - I dont want to do anything except provide some info to the user ,therefore I need somehow to know this when the user click to open the window...Globin
Why do you not want to use window.open then? Since that is a method call, it has a return value you can check. Clicking on a link doesn’t have any return value, so using this method a comparable check would be more complicated (if at all possible.)Interflow
@CBroe - Thanks, so there is a way to use window open for open it in a new process?Globin
Doesn’t Chrome start a new process for new tabs anyway? (And why is it so important that it runs in a new process?)Interflow
@CBroe - Lets say you are doing window.open from first window and want to debug the second window the first window in blocked, if you open new process(like the code I write) it doesn't...any idea how to overcome this?Globin
"While this is working, sometimes the new window/tab is blocked with the pop-up blocker" Can you provide an example of a window or tab being blocked?Escort
@Escort - you can use the code which I provided in the question (from var prod = document.getElementById("myElement"); ...) ,this should open for you and new tab but the problem is that this tab are sometimes blocked...any idea how to at least know by code that this tab is blocked ? I know that there is option to know that when you use window.open but this is not the case for new process :(Globin
@Escort - please see my update2 ...Thanks!Globin
@Escort - did you able to see the issue ? there is some direction which you think that we can handle this issue?Globin
What is the purpose of placing a break point at opened window?Escort
<a href="https://mcmap.net/q/128577/-how-can-i-detect-if-a-browser-is-blocking-a-popup">try this answer!</a> upvote if it works.Santa
A
2

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?

Awl answered 3/10, 2016 at 11:19 Comment(11)
Thanks, This is not solve the problem , please refer to paragraph 3 in this article blog.chromium.org/2008/09/multi-process-architecture.html ,the linked process prevent from the source process to do any operation , i'll update my post soon to explain it better. thank you!Globin
Please see my updated post and see if you can help please, Thank you!Globin
And to me it seems it is more related to the behavior of the browser. Javascript on browser has little control over low level behavior like opening a tab by process or thread. If it is just for debug, how about using Firefox for this specific case?Awl
Yes this is related to chrome and not happen in firefox, but I need to find some solution to chrome (therefore I tagged chrome :) ) , did you able to face the blocking in the first page when debug ? the code I put in the begging of the post (with noreffer & blank )solve it but with it I want to find if the popup blocker is enabled ...any idea how to handle it elegantlly will be very helpful...Thanks!Globin
I am not able to reproduce your problem. The debugger works well for me. (I am using Chrome 53.0.2785.116 m ). I guess maybe an update will solve the problem? Or how about mixing the 2 methods you mentioned? Use the window.open to check if there is a popup block (open a page with a javascript that auto close itself?), then use your second method to open the real window.Awl
This is very strange , this happen in all our team...I check also chrome beta , canary etc this happen in all the chrome versions for sure ..., did you run the program ,click on window open native then put a BP in the CNN tab and when it stops back to the first page to try to click on some button ,this works for you ???? I'll edit the post instructions to be more clear...Globin
you maybe forget to refresh the tab (that opens CNN) until it stops in BP and then go back to the first page and try to click on something....can you please try it and let me know?Globin
I got the problem now, but you may still check for popup using the window.open, immediate window.close and then use your second method to open the the real one, or simply use firefox?Awl
Thanks to open the window and close this is something that I thought about but the problem is that this really open a window in case you don't have the popup blocker (and even if you close it immediately you will see that the tab is opened and closed which can be strange for users) but maybe there is other workaround ? regard firefox /edge yes I know that I can use it but this solution is for end users and should be generic and not for me :) we having program that open windows...Globin
For production, just switch back to window.open? I don't think end users will set break points? Or if you don't want 2 sets of code, just use Firefox for testing this part?Awl
By the way, in the case of break point, I think Chrome purposely block the original window (or the parent window, to be exact). In Javascript, you are able to control the popup window from the parent window, so it makes sense to block the parent windows in case the parent window will make some control on child window based on some other event on parent.Awl

© 2022 - 2024 — McMap. All rights reserved.