Home Web Front-end JS Tutorial Solution to the problem that using window.open to open a new window in Ajax request response is intercepted

Solution to the problem that using window.open to open a new window in Ajax request response is intercepted

Jan 09, 2017 pm 02:15 PM

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:

1. If it is Calling window.open() in the fun method specified in this will not be intercepted, because the browser will think it is active. But opening the window in the response of the ajax request will be intercepted.

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

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

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!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

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

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

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

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

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

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

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

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

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

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

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

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

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

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

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

See all articles