How to open a new window on form submit

后端 未结 9 1886
说谎
说谎 2020-11-29 22:36

I have a submit form and want it to open a new window when users submits the form so i can track it on analytics.

Here is the code I\'m using:



        
相关标签:
9条回答
  • 2020-11-29 23:10

    i believe this jquery work for you well please check a code below.

    this will make your submit action works and open a link in new tab whether you want to open action url again or a new link

    jQuery('form').on('submit',function(e){
    setTimeout(function () { window.open('https://www.google.com','_blank');}, 1000);});})
    

    This code works for me perfect..

    0 讨论(0)
  • 2020-11-29 23:15

    window.open doesn't work across all browsers, Google it and you will find a way of detecting the correct dialog type.

    Also, move the onclick call to the input button for it to only fire when the user submits.

    0 讨论(0)
  • 2020-11-29 23:16

    The code you have given, needs to be corrected. In form tag you have to enclosed the onClick attribute value in double quote:

    "window.open('google.htm','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');"

    You also need to take care that first parameter of window.open should also be enclosed using quotes.

    0 讨论(0)
  • 2020-11-29 23:18

    For a similar effect to form's target attribute, you can also use the formtarget attribute of input[type="submit]" or button[type="submit"].

    From MDN:

    ...this attribute is a name or keyword indicating where to display the response that is received after submitting the form. This is a name of, or keyword for, a browsing context (for example, tab, window, or inline frame). If this attribute is specified, it overrides the target attribute of the elements's form owner. The following keywords have special meanings:

    • _self: Load the response into the same browsing context as the current one. This value is the default if the attribute is not specified.
    • _blank: Load the response into a new unnamed browsing context.
    • _parent: Load the response into the parent browsing context of the current one. If there is no parent, this option behaves the same way as _self.
    • _top: Load the response into the top-level browsing context (that is, the browsing context that is an ancestor of the current one, and has no parent). If there is no parent, this option behaves the same way as _self.
    0 讨论(0)
  • 2020-11-29 23:18

    I generally use a small jQuery snippet globally to open any external links in a new tab / window. I've added the selector for a form for my own site and it works fine so far:

    // URL target
        $('a[href*="//"]:not([href*="'+ location.hostname +'"]),form[action*="//"]:not([href*="'+ location.hostname +'"]').attr('target','_blank');
    
    0 讨论(0)
  • 2020-11-29 23:20

    In a web-based database application that uses a pop-up window to display print-outs of database data, this worked well enough for our needs (tested in Chrome 48):

    <form method="post" 
          target="print_popup" 
          action="/myFormProcessorInNewWindow.aspx"
          onsubmit="window.open('about:blank','print_popup','width=1000,height=800');">
    

    The trick is to match the target attribute on the <form> tag with the second argument in the window.open call in the onsubmit handler.

    0 讨论(0)
提交回复
热议问题