开发者

window.Open Open popup Instead of new window

开发者 https://www.devze.com 2023-02-17 08:10 出处:网络
I have a, ajax function in jQuery and I want in complete function to open a url in a new window or tab.

I have a, ajax function in jQuery and I want in complete function to open a url in a new window or tab.

<a href="#" onclick="Add('1','http://www.google.com');return false;">Link</a>

And :

function Add(ID, 开发者_如何学编程url)
{
    var data = "{'ID' : '" + ID + "'}";
    $.ajax({
        type: "POST",
        url: "Function.ashx/Add",
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        complete: function ()
        {
           window.open(url);
        }
    });
}

window.open function works as popup but I want to open the link in a new window. w3School Example works prefect. But my browser detects it as a popup and blocks it.

What's wrong with my code?


Why do you specifically wait till the AJAX call completes to load the URL?

Why not simply trigger the AJAX call and open the new window directly after.

Example:

<a href="#" onclick="Add('1','http://www.google.com');return false;">Link</a>

 

function Add(ID, url)
{
    var data = "{'ID' : '" + ID + "'}";
    $.ajax({
        type: "POST",
        url: "Function.ashx/Add",
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });

    window.open(url);
}


I'm guessing w3cschool.com probably gets the popup previlige for being an authorised tutoring site.

AFAIK, any means to open an extra window without direct user interaction (e.g. a click) is considered a "popup" and would be blocked by most of the browsers with popup blocker enabled. Your best bet is probably open the link upon a click and inject whatever you want upon the AJAX completion:

<a href="http://www.google.com" onclick="Add('1')">Link</a>

function Add(ID, url)
{
    var newWindow = window.open(url);
    var data = "{'ID' : '" + ID + "'}";
    $.ajax({
        type: "POST",
        url: "Function.ashx/Add",
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        complete: function (data)
        {
           // do whatever you want with newWindow
        }
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消