How do I prevent Google Chrome from blocking my popup?

前端 未结 7 1126
小鲜肉
小鲜肉 2020-11-30 01:21

On my website there is a button that just used to call a function that calls window.open, however, recently an adjustment was needed to do a server-side check b

7条回答
  •  暖寄归人
    2020-11-30 01:41

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

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

提交回复
热议问题