How to open a new window on form submit
Asked Answered
L

9

167

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:

<form action="http://URL at mailchimp subscriber URL.com" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" onclick=window.open(google.html,'','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');>
    <label for="name">Your Name</label><input type="text" value="" name="FNAME" class="required" id="mce-FNAME">
    <br/>
    <br/>
    <label for="email">Your Email </label><input type="text" value="" name="EMAIL" class="required email" id="mce-EMAIL">
    <br/>
    <br/>
    <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="submit">
</form>
</div>
Lecturer answered 22/5, 2009 at 7:30 Comment(0)
B
336

No need for Javascript, you just have to add a target="_blank" attribute in your form tag.

<form target="_blank" action="http://example.com"
      method="post" id="mc-embedded-subscribe-form"
      name="mc-embedded-subscribe-form" class="validate"
>
Bellyache answered 22/5, 2009 at 7:34 Comment(10)
if you add the target=_blank, you don'y need the onClick event.Houseroom
This is still wrong, for multiple reasons. Most of the quoting's missing, and it will fire anytime the form is clicked for any reason, not only at submit.Pipkin
Glad to have stumbled upon this post! Adding target="_blank" to the form tag solved my problem of needing to open a new window!Allargando
this doesn't exactly answer the OP's question, but it is the more useful answer in general- use target="_blank" instead of JavascriptSeymore
target="blank" works just fine. developer.mozilla.org/en-US/docs/HTML/Element/formCripple
at least on IE 11, target="_blank" creates a new tab in the current browser window instead of creating a new browser window..... IMHO, a new tab is different than a new browser windowChapell
If I remember right the target attribute lets you name the window and reuse it _blank means just keep creating new windows. So, target="blank" will be naming the window "blank", if that makes sense.Nevis
Actually, target='_blank' will open a new tab, not a new WINDOW as the original question states.Jonniejonny
The correct solution to "open a new window" is target="_blank". Whether the new window is added as a tab is a browser choice. Many modern browsers deem it a better user experience to add new windows as tabs.Lapidary
If there any way to get the reference to the new window that is created on form submission this way? I need to know when that window is closed.Elsworth
F
46

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.

Foreyard answered 15/2, 2016 at 23:54 Comment(5)
This gives window size control. Excellent.Hezekiah
best solution in this page, tested it and works to perfection.Jonniejonny
This is exactly what I was looking for when generating a PDF into a separate window on submit.Agretha
OMG, memories :) I used that method in Netscape around 1995 ! you could adjust the size of window, how big, even the position.Oribelle
Late to the party but I just spent 3 days trying to get popup windows using JavaScript to submit necessary behind the scenes jQuery values to my database, something which is plain trivial to do in PHP. (I'm building a roll-your-own blog, a page with multiple submit buttons). This is exactly the solution I've been looking for. Mucho thanks!Barre
L
14

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.
Lowry answered 18/3, 2016 at 16:37 Comment(2)
Very useful when having multiple submit buttons.Griffin
Looks like they removed that content from the input element page. Similar info about the formtarget attribute can be found on the button element page: developer.mozilla.org/en-US/docs/Web/HTML/Element/…Foreland
I
9

onclick may not be the best event to attach that action to. Anytime anyone clicks anywhere in the form, it will open the window.

<form action="..." ...
    onsubmit="window.open('google.html', '_blank', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');return true;">
Imprudent answered 22/5, 2009 at 14:37 Comment(0)
E
4

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.

Emotion answered 22/5, 2009 at 7:43 Comment(0)
C
2

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');
Certificate answered 27/11, 2014 at 16:20 Comment(0)
B
0

Thumbs up to @Awerealis for an authentic, php popup window solution.

But after getting the popup window and using it to insert data into my database, I ran into the further problem of having the results page of the db entry fill up the popup window. I wanted the popup window to go away and have the original (parent) page refresh with the new data.

I achieved that with the following:

On the parent page (from which the popup window is issued), I added this code to the <head> element:

<script>
    window.onunload = refreshParent;
    function refreshParent() {
        window.opener.location.reload();
        window.close();
    }
</script>

At the end of the popup (child) window, I added:

<script type="text/javascript">
  window.close();
</script>

Upon returning to the parent page, the new data is displayed giving altogether the feeling of an ajax call from the popup window which is what I was looking for.

Hope this helps someone.

Barre answered 17/5, 2023 at 20:56 Comment(0)
P
-2

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..

Proscribe answered 28/8, 2019 at 7:10 Comment(0)
H
-4

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.

Hoon answered 22/5, 2009 at 7:34 Comment(1)
onclick on the input button is wrong. Then, if the user clicks it, but moves away before releasing, it will still fire.Pipkin

© 2022 - 2024 — McMap. All rights reserved.