Home >Web Front-end >JS Tutorial >How to set up Ajax request to open a new window immediately after success
This time I will show you how to set up an Ajax request to open a new window immediately after a successful request. What are the precautions for setting up an Ajax request to open a new window immediately after a successful request. The following is a practical case, let's take a look.
Without further ado, the key code is as follows:
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 , unable to open 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 see how to open a new window after the Ajax response Window
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. User login After that, open the Url pointed to by the link 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 (tab) in the callback, which will be considered malicious by the browserBehavior.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Use ajax to verify whether the registered user name exists
ajax sends data type step to the server Detailed explanation
The above is the detailed content of How to set up Ajax request to open a new window immediately after success. For more information, please follow other related articles on the PHP Chinese website!