Submit a Form to a New Window

By  on  

There are certain times when it's important to open a link in a new window. The short and effective way of achieving that is adding the 'target="_blank"' attribute/value to your anchor tag. But what about when you want form submission to have the same effect -- a new window? Luckily, you can do the same thing:

The XHTML

<form action="http://othersite.com" method="post" target="_blank">

	<-- form fields here -->
	
</form>

Of course, the Target-Blank haters may not approve of this method, but it's quick and always works.

Recent Features

  • By
    6 Things You Didn&#8217;t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    5 Awesome New Mozilla Technologies You&#8217;ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

Incredible Demos

  • By
    DWRequest: MooTools 1.2 AJAX Listener &#038; Message Display

    Though MooTools 1.2 is in its second beta stage, its basic syntax and theory changes have been hashed out. The JavaScript library continues to improve and become more flexible. Fellow DZone Zone Leader Boyan Kostadinov wrote a very useful article detailing how you can add a...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Discussion

  1. Iv never need to do this, and actually never really thought about it, but im glad to see that, that would work if i ever needed to do it :D Cheers David.

  2. I’m afraid that I’m a Target-Blank hater, but fortunately, not so hardcore that I refuse to use it when it’s the only thing that works. I wasn’t aware that you could do it with forms, will add that to my arsenal, thanks David!

  3. A word of warning: target is not a valid attribute in XHTML.
    So while it works, it isn’t specified in the standard anymore.

  4. @Rexxars – that’s why I’m against it

  5. Rich

    What about making something like this and adding it to the form:

    this is JQuery

    $('a[rel="external"]').click( function() {
            window.open( $(this).attr('href') );
            return false;
        });
    
  6. Fabian Beiner

    Yeah, I would also do something like:

    function submitForm(formId) {
        newDialog = window.open('about:blank', "_form");
        document.forms[formId].target='_form';
        document.forms[formId].submit();
        return false;
    }
    
    <form id="loginform" action="#" method="post" onsubmit="return submitForm(this)"></form>
    

    I didn’t test it at the moment, but I guess it should work ;)

  7. Fabian Beiner

    Doh, no idea why, but my post didn’t work :( Anyway, I would try something like that: http://nopaste.ch/62b4538ef17e6da.html – but I didn’t test it, yet…

  8. @All: I understand that “target” is no longer considered valid, but it’s short and effective. It also works when the user has JS disabled.

    @Rich: I don’t know if it’s possible to apply that to an external window.

  9. Rich

    Maybe i wasn’t clear in my comment, but your saying something like this would not work?
    @david

    $(’a[rel="external"]‘).click( function() {
    window.open( $(this).attr(’href’) );
    return false;
    });
    
    form action="http://othersite.com" method="post" rel="external" 
    
    <-- form fields here -->
    

    obviously placing the jquery code in the dom. and i removed the open and close from that form tag because it wasnt showing up in the preview.

  10. @Rich: Yep, I know where you’re going with that, but I’m not sure if the javascript will pass the POST form information on.

  11. thom

    excellent timing; i needed this at work today. cheers!

  12. I know that target isnt valid anymore, but can someone educate me as to what i would use instead? and maybe i could join the darkside too?

  13. Rich

    @david gotcha. i think your right. I just tried it a couple of different ways with no results. could be something interesting to think about though. although i only tried for about 10 min..lol

    Good post though.

  14. That’s a cool trick. I’m a little bit of a fledgling at website development and tried to use target=”blank” and I was pretty damn happy to find out that all I needed was an underscore! Thanks alot David!

  15. Try this: onsubmit=”this.target=’_blank'”

    A JQuery wich tests “rel” attribute will fail in validation too, because this attribute isn’t on the list for the element “form”: http://www.w3.org/TR/html401/interact/forms.html#h-17.3

    Must be a way to assign the target to a class using JQuery.

    Hope it helps

  16. Geert

    I want a smaller new window instead of opening a new tab,
    so I’ve used this for a website, the only problem is that it doesn’t seem to work in IE 9

    Javascript:

    function sendme() 
    { 
        window.open("","myNewWin","width=500,height=300,toolbar=0"); 
        var a = window.setTimeout("document.form1.submit();",500); 
    } 
    
  17. Derby Russell
        /** This is the script that will redraw current screen and submit to paypal. */
        echo ''."\n" ;
        echo 'function serverNotifySelected()'."\n" ;
        echo '{'."\n" ;
        echo '    window.open(\'\', \'PayPalPayment\');'."\n" ;
        echo '    document.forms[\'paypal_form\'].submit();'."\n" ;
        echo '    document.forms[\'server_responder\'].submit();'."\n" ;
        echo '}'."\n" ;
        echo ''."\n" ;
    
        /** This form will be opened in a new window called PayPalPayment. */
        echo ''."\n" ;
        echo ''."\n" ;
        echo ''."\n" ;
        echo 'hosted_button_id.'">'."\n" ;
        echo ''."\n" ;
        echo '    '."\n";
        echo '        hide_name_a.'" value="'.$single_product->hide_value_a.'">Local'."\n" ;
        echo '    '."\n" ;
        echo '    '."\n" ;
        echo '        '."\n" ;
        echo '        hide_name_b.'" value="'.$single_product->hide_value_b.'" />'.$single_product->short_desc.' $'.$adj_price.' USD'."\n" ;
                        // 
                        //     1 Day $1.55 USD
                        //     All Day $7.50 USD
                        //     3 Day $23.00 USD
                        //     31 Day $107.00 USD
                        // 
        echo '        '."\n" ;
        echo '    '."\n" ;
        echo ''."\n" ;
        echo ''."\n" ;
        echo ''."\n" ;
    
        /** This form will redraw the current page for approval. */
        echo ''."\n" ;
        echo ''."\n" ;
        echo 'product_id.'">'."\n" ;
        echo ''."\n" ;
        
        /** No form here just an input and a button.  onClick will handle all the forms */
        echo ''."\n" ;
        echo ''."\n" ;
    
    
    
  18. It worked for me. My WP theme was sent back as I was not using _blank target. Thanks for sharing.

  19. I can’t make it work with Safari on macOS.
    I am submitting through javascript though:
    $(someformselector).submit();

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!