Avoid browser popup blockers

后端 未结 9 1917
一个人的身影
一个人的身影 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:39

    In addition Swiss Mister post, in my case the window.open was launched inside a promise, which turned the popup blocker on, my solution was: in angular:

    $scope.gotClick = function(){
    
      var myNewTab = browserService.openNewTab();
      someService.getUrl().then(
        function(res){
            browserService.updateTabLocation(res.url, myNewTab);
    
        }
      );
    };
    

    browserService:

    this.openNewTab = function(){
         var newTabWindow = $window.open();
         return newTabWindow;
    }
    
    this.updateTabLocation = function(tabLocation, tab) {
         if(!tabLocation){
           tab.close();
         }
         tab.location.href = tabLocation;
    }
    

    this is how you can open a new tab using the promise response and not invoking the popup blocker.

    0 讨论(0)
  • 2020-11-22 12:40

    I tried multiple solutions, but his is the only one that actually worked for me in all the browsers

    let newTab = window.open(); newTab.location.href = url;

    0 讨论(0)
  • 2020-11-22 12:42

    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.

    0 讨论(0)
提交回复
热议问题