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

javascript - How do I prevent browsers from blocking the pop-up window that I've created? - Stack Overflow

programmeradmin5浏览0评论

I created a simple JavaScript function to display my pop-up window once it loads. But it keeps on being blocked by Firefox and Google Chrome and I have to somehow enable it on the Firefox and Chrome to display the pop-up.

Are there any alternatives for this?

I have a player on the pop-up window so I have to use a pop-up to let the player play automatically. The problem is that if I put it on the page itself, once the user clicks another page the entire page reloads and the player automatically stops for a few seconds until the whole page reloads and I have to prevent this from happening.

I created a simple JavaScript function to display my pop-up window once it loads. But it keeps on being blocked by Firefox and Google Chrome and I have to somehow enable it on the Firefox and Chrome to display the pop-up.

Are there any alternatives for this?

I have a player on the pop-up window so I have to use a pop-up to let the player play automatically. The problem is that if I put it on the page itself, once the user clicks another page the entire page reloads and the player automatically stops for a few seconds until the whole page reloads and I have to prevent this from happening.

Share Improve this question edited Jan 31, 2019 at 22:46 pnuts 59.6k11 gold badges91 silver badges141 bronze badges asked Jul 25, 2014 at 2:33 Agent SmithAgent Smith 231 silver badge7 bronze badges 5
  • 6 You can't. You can change your popup so that instead of a window it uses a floating div – jasonscript Commented Jul 25, 2014 at 2:37
  • I see but I can't. Once he/she switches to another page, the floating div refreshes also. – Agent Smith Commented Jul 25, 2014 at 2:45
  • 1 If they switch to another page then they don't want to see your content – jasonscript Commented Jul 25, 2014 at 2:46
  • You need to tell the problem to either "enable pop ups for this domain" or figure out a different solution like framing your site like people did in the 90's or learn about this 10 year old technology called Ajax with a single page app. – epascarello Commented Jul 25, 2014 at 3:16
  • Actually, that would be pretty useful and I've remended it in the first place but they won't accept it. They say that some other porn websites have these pop-ups that the browser didn't block upon loading the porn page. – Agent Smith Commented Jul 25, 2014 at 3:45
Add a ment  | 

3 Answers 3

Reset to default 2

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.

Please take a look at dthorpe's answer here. It covers your question.

You could try putting the player on the original page, and using something like History.js to control page changes (you could have the main page body in one wrapper div that changes, and leave the player outside of it).

Otherwise, you could try (assuming you meant a HTML5 <video> or <audio> player) downloading the data to localStorage/cookie/[other persistent storage mechanism] and have it seek everytime you change a page.

It will be hard to stop browsers from blocking your pop up window, because any way to do so is inherently exploitable; however, if you call the function to open another window from an onclick event, you may be able to circumvent some popup blockers. Also, some popup blockers allow popups when using the https protocol, although not many have this feature, and https can be hard to implement for the average website, if you don't have physical access to the server.

One other option is to open the other page in another tab (like this w3c example; you can 'click' the link with javascript).

You might also want to look at this post, as it is somewhat similar.

I only just discovered you asked this question.

Here's the answer in full.

Basically, you can simply create the popup immediately as the user event is fired, then fill it with content (your player, for instance) as you have it available.

发布评论

评论列表(0)

  1. 暂无评论