Pop up blocker API- how to check if user has it enabled

后端 未结 5 994
野趣味
野趣味 2021-02-04 00:37

I need to know when the user clicks on the button that triggers window.open if there is stable API/way to know beforehand if the user actively has

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 01:13

    Well this is your question - I need some stable solution how to know when the user click on event which open window ,how to know if he have the pop-up blocker enabled . before the window is open...thanks!

    I am afraid there isn't any method to handle that. I researched upon this same issue a while back and I am sharing what I found.

    Why our popups are blocked?

    Smart popup blockers will allow a popup if it is directly associated to a user’s action. If it’s delayed in anyway, there’s a good chance it’s going to get blocked.

    Reference : Andy Stratton (His Blog)

    I like this explanation more

    The general rule is that popup blockers will engage if window.open or similar is invoked from javascript that is not invoked by direct user action. That is, you can call window.open in response to a button click without getting hit by the popup blocker, but if you put the same code in a timer event it will be blocked. Depth of call chain is also a factor - some older browsers only look at the immediate caller, newer browsers can backtrack a little to see if the caller's caller was a mouse click etc. Keep it as shallow as you can to avoid the popup blockers.

    Reference : dthorpe (Stack Overflow Username)

    What we can do?

    One thing is crystal clear as of now that there isn't any direct way to tweak with pop up blockers from code. I think the reason behind it is, it'll hamper the only reason behind it's existence.

    You can go read at the solution presented by Andy on his blog here : Click. If I had to write what he has explained in short I'd directly say that use popups for response section. Andy explains that we can use windows.open in response section after a call is directly made on user's action wont be blocked by popup blockers.

    Like xaxxon has explained it is possible to check after execution of windows.open that if it was blocked or not. Generally that is what people do and developers while making any extra service feature keep that in mind. For example I was implementing twitter's digits authentication system. I used to check for message after failure in execution of pop up command and then I could show a message to user to enable pop up window but then I found the solution mentioned above. It makes the whole thing more neat and clean.

提交回复
热议问题