How to set up Ajax request to open a new window immediately after success

php中世界最好的语言
Release: 2018-04-03 17:35:29
Original
2208 people have browsed it

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 theprecautionsfor 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"); } } });
Copy after login

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"); }
Copy after login

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 loginAfter 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; });
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!