JS click an element to send an ajax request to open a new window

Source: Internet
Author: User

JS click element send ajax request open a new window often encounter such a requirement in the project, click an element, need to send ajax request, after the request is successful, the development needs to pass the link to the front-end (or open a new window after the request is successful), and the front-end needs to open the link through a new window. In fact, this principle may be very simple and easy to think, use window. open can open a new window, or click the div element. After an ajax request is sent successfully, the link is dynamically transmitted to the tag, and then the tag event is triggered, right? Once upon a time, I sent such a problem to the JS group. Many JS community friends said that long can't achieve this simple problem? How are you working on the front-end? Everyone thought it was very simple. After more than an hour of discussion, there was no result! Many people propose window. open. Someone else uses location. href = "" then set target = "_ blank" and so on. It is also suggested to use form to submit, but no matter how they use it, the results are the same, in the mainstream browsers firefox and chrome, the result is: it was intercepted by the browser! (Ie I Don't Care). Normally, it's okay to click an element and open a new webpage with window. open, and it won't be intercepted by the browser! Everyone knows this, but why do I need to send ajax requests one more step and the requests are intercepted by the browser? To solve this problem, you must make a demo. I did this: <div class = "testA" style = "cursor: pointer; "> click I to bring up a new window </div>. JS: copy the code $ ('. testA '). unbind ('click '). bind ('click', function () {$. ajax ({url: 'http: // localhost/demo/windowopen/test. php ', 'type': 'post', ype: 'json', success: function (data) {if (data & data. success) {window. open ('HTTP: // www.baidu.com ') ;}}) ;}); copy the code URL. I can either make a request or use the same domain, this is an asynchronous ajax request. To solve this problem, we can And firefox and chrome won't be blocked! The JS Code is as follows: copy the code $ ('. testA '). unbind ('click '). bind ('click', function () {$. ajax ({url: 'http: // localhost/demo/windowopen/test. php ', 'type': 'post', async: false, dataType: 'json', success: function (data) {if (data & data. success) {window. open ('HTTP: // www.baidu.com ') ;}}) ;}); copy the code to set the synchronous request. (async: false ). Everyone can test it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.