简体   繁体   English

跟踪为什么window.open不起作用

[英]Track down why window.open does not work

Other attempts to use window.open('url', '_blank') within this script work as desired. 根据需要尝试在此脚本中使用window.open('url', '_blank')其他尝试。 This one, not opening a new tab, uses different logic following a confirm() prompt. 这个,而不是打开新的选项卡,在confirm()提示后使用不同的逻辑。 The logic is to display a portion of the page if 'Reserved' is found in it or to redirect to the target page otherwise. 逻辑是在页面中找到“保留”时显示页面的一部分,否则重定向到目标页面。

The redirect is automatic as a result of the $.ajax success handler. $.ajax成功处理程序的结果是重定向是自动的。 Neither method of redirect makes any changes. 两种重定向方法都不会进行任何更改。

form for 2nd method: 第二种方法的表格:

<form method=\"get\" id=\"formgoregistry\" name=\"goregistry\" action=\"\" target=\"_blank\">
</form>

javascript function in question: 有问题的javascript函数:

function getRegistryInfo(num) {
    // for 2nd method, set the form's action
    $('#formgoregistry').attr('action', 'http://somesite.com/somefile?n=somevalue');

    var response = $.ajax({
        type: 'POST',
        url: 'purchases_registry.php',
        dataType: 'html',
        data: { number: num },
        success: function(data) {
            console.log('response = '+data);
            if (data.indexOf('Reserved') > 0) {
                //  work with the result
                //  Code in here works fine
                ....
            }
            else {
                // wrong data, redirect to outside page
                //  **  Neither of these method open a new tab
                // 1st method
                var url = 'http://somesite.com/somefile?n=somevalue';
                console.log('url = '+url);                // prints the correct url
                var w = window.open(url, '_blank');       // NO new tab or error
                console.log('window = '+w);               // window = undefined

                // 2nd method (setting form's action beforehand)
                console.log(document.goregistry.action);  // prints the correct url
                document.goregistry.submit();             // NO new tab or error
            }
        }
    });
}

I am wondering if the window being undefined is part of the problem and why. 我想知道是否未定义窗口是问题的一部分,为什么。

Use this script to open the link in new window : 使用此脚本在新窗口中打开链接:

echo "<form action=\"your_link.php?id=\" method=\"post\" target=\"myWindow\", onsubmit=\"window.open('', this.target, 'width=300,height=400,resizable=no,scrollbars=no');return true;\">

or in HTML language : 或使用HTML语言:

<form action="your_link.php?idk=$id=" method="post" target="myWindow", onsubmit="window.open('', this.target, 'width=300,height=400,resizable=no,scrollbars=no');return true;">

If you using a href method just simply change onsubmit to onclick, that's all I know, hope it helps, good luck! 如果您使用href方法,只需将onsubmit更改为onclick,这就是我所知道的,希望对您有所帮助,祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM