window.open without popup blocker using AJAX and manipulating the [removed]

后端 未结 2 1982
北荒
北荒 2021-01-31 12:23

When dealing with OAuth from the server, such as Twitter and Facebook, you most likely will redirect the user to an URL asking for app permission. Usually, after clicking a link

相关标签:
2条回答
  • 2021-01-31 12:55

    Try adding async: false. It should be working

    $('#myButton').click(function() {
    $.ajax({
        type: 'POST',
        async: false,
        url: '/echo/json/',
        data: {'json': JSON.stringify({
            url:'http://google.com'})},
        success: function(data) {
            window.open(data.url,'_blank');
        }
    });
    });
    
    0 讨论(0)
  • 2021-01-31 13:04

    The answer is quite simple, and works cross browser without any issues. When doing the AJAX call (I'll be using jQuery in this example), just do the following. Suppose we have a form with two buttons, Login with Twitter and Login with Facebook.

    <button type="submit" class="login" value="facebook" name="type">Login with Facebook</button>
    <button type="submit" class="login" value="twitter" name="type">Login with Twitter</button>
    

    Then the Javascript code where the magic happens

    $(function () {
        var
            $login = $('.login'),
            authWindow;
    
        $login.on('click', function (e) {
            e.preventDefault();
            /* We pre-open the popup in the submit, since it was generated from a "click" event, so no popup block happens */
            authWindow = window.open('about:blank', '', 'left=20,top=20,width=400,height=300,toolbar=0,resizable=1');
            /* do the AJAX call requesting for the authorize URL */
    
            $.ajax({
                url: '/echo/json/',
                type: "POST",
                data: {"json": JSON.stringify({"url": 'http://' + e.target.value + '.com'})}
                /*Since it's a jsfiddle, the echo is only for demo purposes */
            })
            .done(function (data) {
                /* This is where the magic happens, we simply redirec the popup to the new authorize URL that we received from the server */
                authWindow.location.replace(data.url);
            })
            .always(function () {
                /* You can poll if the window got closed here, and so a refresh on the main page, or another AJAX call for example */
            });
        });
    });
    

    Here is the POC in JSFiddle http://jsfiddle.net/CNCgG/

    This is simple and effective :)

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