


Solution to the problem that using window.open to open a new window in Ajax request response is intercepted
1. Problem description
After the asynchronous asynchronous request is successful, a new window needs to be opened to open the url. The window.open() method is used, but it will be intercepted by the browser. The user needs to click Down.
2. Problem Analysis
The reason why the browser intercepts newly opened windows is because the operation is not actively triggered by the user, so it thinks it is unsafe. Intercepted, even if the user behavior such as click or submit is simulated in the ajax callback function (trigger('click')), the browser will think that it is not actively triggered by the user, so it cannot be executed safely, so it is intercepted.
Instructions:
2. If instead of opening a new window, you change the original web page address, you can use window.location = newurl to achieve this, so that it will not be intercepted.
3. Solution
Before ajax request, use window.open to open a blank window, and then set the location attribute of the window in the ajax response function for the new url.
Code examples such as:
function fun(){ var tmpWin =window.open() ajax(xxx, handle(){ //回调函数。这是伪代码,语法不准。 var newurl = xxxx tmpWin.location = newurl; }) }
There is a problem with the above method, because a blank window is opened first. If the ajax request After a failure (network or business logic problem), there will be no normal results reflected in the new window, which may cause confusion to the user.
One solution is that when there is a problem with ajax, you can consider giving a prompt, such as tmpWin.document.write("Server processing exception");
Even to prevent ajax response time Too long. When the window is created, a prompt will be given immediately tmpWin.document.write("The server is processing, please wait");
If ajax returns normally later, it will be because the location is set value, the original printed information will be overwritten by the new page information.
There is another method here, but it also has flaws:
Because ajax can be set as a synchronous request, you can use window.open to open a new window after the ajax request. . For example:
function fun(){ var result; ajax({ //需要设置同步请求 ..... result = xxx ....... }) if(result){ window.open(xxxx) } }
The above approach avoids the problems mentioned above because it opens a new window after judging the result of the ajax request.
But because it is a synchronous request, we found a problem in our test. If the server response time is too long, firstly, the interface will pause (the user experience is not good), and secondly, the new window will be blocked.
There is no problem only if the server returns quickly. During our test, we slept for 1 second during server code processing and found that the new window was blocked.
4. Summary
In summary, it can be seen that there is no particularly perfect method for opening a new window after ajax returns. Specifically, you need to take appropriate measures based on the business characteristics of your own system.
The above is the solution introduced by the editor to you when using window.open to open a new window in the Ajax request response and being intercepted. I hope it will be helpful to you. If you have any questions, please contact us. I will leave a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!
For more related articles on solutions to the interception of using window.open to open a new window in the Ajax request response, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig
