Home  >  Article  >  Web Front-end  >  Open a new window address after the Ajax request is successful

Open a new window address after the Ajax request is successful

亚连
亚连Original
2018-05-23 15:45:462679browse

This article mainly introduces the relevant information on the address of opening a new window after the Ajax request is successful. It is very good and has reference value. Friends in need can refer to it

Without further ado, the key code is as follows Display:

jQuery.ajax({
"type":"post",
"url":"http://www.baidu.com", 
"success":function(rel){
if(rel.isSuccess){ 
window.open(rel.url,"_blank");
}
}
});

After the url request is successful, window.open(rel.url,"_blank"); will be intercepted by the browser and cannot be opened. For a new window, if you put window.open() outside ajax, the problem will be solved, the code is as follows:

var result="";
jQuery.ajax({
"type":"post",
"url":"http://www.baidu.com", 
"success":function(rel){
if(rel.isSuccess){ 
result=rel.url;
//window.open(rel.url,"_blank");
}
}
});
if(result.length>0){
window.open(result,"_blank");
}

Let’s look at opening a new window after the Ajax response

There is a function in recent development. After clicking a link, it is necessary to determine whether the current user is logged in. If not, A login dialog box needs to pop up. After the user logs in, the URL pointed to by the link is opened in a new window (tab).

Not much to say, just post the code:

$(document).delegate("a", "click", function () { 
var actionUrl = $(this).attr("href"); 
var ssoAction = function () { window.open(actionUrl, '_blank'); }; 
if (isLogin()) { 
ssoAction(); 
} else { 
popup.show({login:function () { 
$.ajax({ 
type: "post", 
dataType: "json", 
url: "/Account/Login", 
data: $("frmLogin").serialize(), 
//发送方式改为同步,避免弹出页面被浏览器拦截
async: false, 
success: function (oData) { 
ssoAction(); 
} 
}); 
}); 
} 
return false; 
});

Key point: You need to use synchronous submission, use asynchronous submission, and open a new window in the callback (tag), will be considered malicious by the browser.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Uploading files through Ajax Using FormData for Ajax requests

jQuery Ajax method for uploading files

Use ajax to implement asynchronous refresh request

##

The above is the detailed content of Open a new window address after the Ajax request is successful. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn