Avoid browser popup blockers

后端 未结 9 1995
一个人的身影
一个人的身影 2020-11-22 12:15

I\'m developing an OAuth authentication flow purely in JavaScript and I want to show the user the \"grant access\" window in a popup, but it gets blocked.

How can I

9条回答
  •  余生分开走
    2020-11-22 12:29

    My use case: In my react app, Upon user click there is an API call performed to the backend. Based on the response, new tab is opened with the api response added as params to the new tab URL (in same domain).

    The only caveat in my use case is that it takes more for 1 second for the API response to be received. Hence pop-up blocker shows up (if it is active) when opening up URL in a new tab.

    To circumvent the above described issue, here is the sample code,

    var new_tab=window.open()
    axios.get('http://backend-api').then(response=>{
        const url="http://someurl"+"?response"
        new_tab.location.href=url;
    }).catch(error=>{
        //catch error
    })
    

    Summary: Create an empty tab (as above line 1) and when the API call is completed, you can fill up the tab with the url and skip the popup blocker.

提交回复
热议问题