php - window.open() popup blocked.
PHPz
PHPz 2017-05-16 13:12:48
0
8
712

Pass a url from the backend, and after receiving it, the frontend will open a new window and jump to this link.
Use window.open(url, '_blank'); to be intercepted by the browser.< /p>

Is there any way to achieve this? I tried simulating the click of the a tag and it still intercepted it. The pop-up p failed to load the iframe, and the window must be opened.

The problem has been solved, see the accepted answer.

PHPz
PHPz

学习是最好的投资!

reply all(8)
巴扎黑

For this question, we must first understand why it is intercepted. The browser will make "non-artificial" callswindow.open的弹窗进行拦截,什么叫做"非人为"的,就是浏览器判断在用户操作和window.open之间不是连续的,浏览器会认为可能是流氓程序弹出的广告窗口从而进行拦截
从楼主的描述可以看出,楼主应该是进行某个操作之后,通过ajax去获取一个url,然后通过window.open打开页面,我们都知道ajax是异步的,浏览器就认为不是连续的,所以就拦截了
解决方法可以先在操作(比如点击)的时候,直接window.open, and then go to ajax to obtain the data, and then assign a URL to the opened window. The approximate pseudo code is as follows:

el.addEventListener('click',function(){
    var winHandler = window.open("","_blank");
   ajax(function(url){
        winHandler.location.href = url;
   });
    
})
过去多啦不再A梦
<a href="http://www.baidu.com" target="_blank" id="xx">hahah</a>
    <button type="button" id='btn'>clickme</button>
    <script>
        document.getElementById('btn').onclick = function() {
            document.getElementById('xx').click();
        }
    </script>

There’s nothing wrong with it.

仅有的幸福

Why add _blank?

window.open('www.baidu.com')
小葫芦

If the url has an http or https header

漂亮男人

Write the event that triggers the method as an a tag, write the logic of obtaining the URL in the Controller pointed to by the a tag, and then return to a new page, bypassing the window.open method in short

过去多啦不再A梦

For security reasons, the browser will automatically block pop-ups

我想大声告诉你

Only a command that opens a new window within a short period of time after the user clicks it, such as window.open, will not be intercepted. This is a security restriction of the browser.

给我你的怀抱

Try adding one more parameter to the ajax request:async: false

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template