Avoid browser popup blockers

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

    Based on Jason Sebring's very useful tip, and on the stuff covered here and there, I found a perfect solution for my case:

    Pseudo code with Javascript snippets:

    1. immediately create a blank popup on user action

      var importantStuff = window.open('', '_blank');
      

      Optional: add some "waiting" info message. Examples:

      a) An external HTML page: replace the above line with

      var importantStuff = window.open('http://example.com/waiting.html', '_blank');
      

      b) Text: add the following line below the above one:

      importantStuff.document.write('Loading preview...');
      
    2. fill it with content when ready (when the AJAX call is returned, for instance)

      importantStuff.location.href = 'http://shrib.com';
      

    Enrich the call to window.open with whatever additional options you need.

    I actually use this solution for a mailto redirection, and it works on all my browsers (windows 7, Android). The _blank bit helps for the mailto redirection to work on mobile, btw.

    Your experience? Any way to improve this?

提交回复
热议问题