简体   繁体   English

通过JS在新标签页中有条件地打开网址

[英]Conditionally open url in new tab via js

I have a survey-like web application where a user selects a number of checkboxes and then presses "Submit" button-like link 我有一个类似调查的Web应用程序,其中用户选择了许多复选框,然后按“提交”按钮状的链接

<!-- page: /survey -->
<form id="survey" method="post" action="/complete">
    <!-- checkboxes, etc here -->
    <a id="submit" class="button" href="#">Submit</a>
</form>
<script>
    $('#submit').on('click', function (e) {
        e.preventDefault();
        //... other staff happens here
        $('#survey').submit();
    });
</script>

Then the form is processed on the server and results are shown to the user on /complete page. 然后在服务器上处理该表单,并在/complete页面上将结果显示给用户。

What I want to achieve is to conditionally open a new tab after form was submitted. 我要实现的是在提交表单后有条件地打开一个新选项卡。 This condition and the url of the tab will be determined on the server after the survey was submitted. 提交调查后,将在服务器上确定此条件和选项卡的url。

I can make it work unconditionally if I plug window.open('...', '_blank') into the click-event handler on the initial /survey page: 如果将window.open('...', '_blank')插入初始/survey页面上的click-event handler中,则可以使其无条件工作:

$('#submit').on('click', function (e) {
    e.preventDefault();
    //... other staff happens here
    window.open('http://example.com', '_blank'); // <---- this line
    $('#survey').submit();
});

It fails though, if I use window.open on the /complete page: a popup warning appears instead. 但是,如果我在/complete页面上使用window.open ,它将失败:而是出现一个弹出警告。 Probably because it is not connected to any user-initiated event. 可能是因为它未连接到任何用户启动的事件。

In my case, however, there is a user-initiated event, but it happened on a previous page. 但是,就我而言,有一个用户启动的事件,但它发生在上一页。 Is there any way I can pass this event to the new page (obviously new page is on the same domain name, protocol, etc). 有什么办法可以将此事件传递给新页面(显然,新页面位于相同的域名,协议等上)。 Probably there is another way? 可能还有另一种方法?

The right way to do what you're after is to change the markup: 做您想做的正确方法是更改​​标记:

<form id="survey" method="post" action="/complete">
    <!-- checkboxes, etc here -->
    <button type="submit" id="submit" class="button">Submit</button>
</form>

And then in JavaScript, listen for the submit event. 然后在JavaScript中,监听submit事件。 This will allow native features—such as implicit submission (enter key in a text field), or middle-clicking on the submit button—to work by default. 默认情况下,这将允许本机功能(例如隐式提交(文本字段中的输入键)或单击提交按钮的中键)起作用。

Now here're some important parts: 现在这里是一些重要的部分:

  • only prevent the default behavior if the input is invalid 仅在输入无效时阻止默认行为
  • if you want the form to open in a new tab, change the target attribute of the form. 如果您希望表单在新选项卡中打开,请更改表单的target属性。
$('#survey').on('submit', function (e) {
    if (!checkValidity(...)) {
        e.preventDefault();
        return;
    }

    //other stuff happens

    var newTab = shouldOpenInNewTab(...);
    $(this).attr('target', newTab ? '_blank' : '');
});

Doing that will allow the browser to use its default behaviors. 这样做将允许浏览器使用其默认行为。 It's cross-browser compatible, and it's accessible. 它与跨浏览器兼容,并且可以访问。

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

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