Home >Web Front-end >JS Tutorial >How to open a new window in jquery

How to open a new window in jquery

王林
王林Original
2020-11-25 11:24:363947browse

How to open a new window in jquery: first create a form; then open a new window through form submission, such as [form.target = ‘_blank‘; form.method = ‘POST‘;].

How to open a new window in jquery

The operating environment of this tutorial: Windows 10 system, jquery version 2.2.4. This method is suitable for all brands of computers.

(Learning video sharing: jquery video tutorial)

There are several ways to open a new window in jquery:

The first one: Create a form Form, a new tab page is opened through form submission.

var form = document.createElement(‘form‘);
form.action = ‘www.baidu.com?id=1‘;
form.target = ‘_blank‘;
form.method = ‘POST‘;
document.body.appendChild(form);
form.submit();

The second type:

var tempwindow=window.open('_blank');
tempwindow.location='www.baidu.com' ;

The third type:

setTimeout(window.open('www.baidu.com'), 500);

The fourth type: (similar to the first type)

/* 在新窗口中打开 */
function openNewWindow(url) {
    var a = document.createElement('a');
    a.setAttribute('href', url);
    a.setAttribute('target', '_blank');
    var id = Math.random(10000, 99999);
    a.setAttribute('id', id);
    // 防止反复添加
    if (!document.getElementById(id)) {
        document.body.appendChild(a);
    }
    a.click();
}
//方法调用
openNewWindow('www.baidu.com');

Note: The above methods are applicable to js. If the page jump action is after the ajax method is executed, ajax synchronous execution needs to be set. Add attribute: async:false. Such as:

$.ajax({
                type: 'post',
                data: param,
                url: 'ActionUrl',
                dataType: 'json',
                async:false,
                success: function (msg) {
openNewWindow('www.baidu.com');
}

Related recommendations: js tutorial

The above is the detailed content of How to open a new window in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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