search

Home  >  Q&A  >  body text

php - window.open() popup blocked.

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.

PHPzPHPz2823 days ago798

reply all(8)I'll reply

  • 巴扎黑

    巴扎黑2017-05-16 13:14:48

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

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:14:48

    <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.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 13:14:48

    Why add _blank?

    window.open('www.baidu.com')

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 13:14:48

    If the url has an http or https header

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 13:14:48

    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

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:14:48

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

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:14:48

    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.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:14:48

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

    reply
    0
  • Cancelreply