How to send a form to a new window?

I want to dynamically insert a form using JavaScript, and then submit it and open the target in a new window.

Here is my code:

            var form = document.createElement("form");
            form.setAttribute("method", "post");
            form.setAttribute("action", xxx);
            form.setAttribute("onsubmit", "window.open('about:blank','Popup_Window','scrollbars=yes,width=550,height=520');");
            form.setAttribute("target", "Popup_Window");

            document.body.appendChild(form);
            form.submit();

This code works - it inserts the form dynamically and successfully submits the form. However, the form opens in a new tab, while I want it to open in a new window. Any idea what is going on? Any suggestions to fix this? I am also open to jQuery.

Thank!

EDIT: I'm not sure why people mark this as a duplicate. Yes, this is about the same topic as in the other question, but the answers are not related - the stated duplicate problem had a new line that messed up the code, I not only, but my code still won’t work ...

+4
source share
4 answers

, , , . , onsubmit:

var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", xxx);

function submitToPopup(f) {
    var w = window.open('', 'form-target', 'width=600, height=400, any-other-option, ...');
    f.target = 'form-target';
    f.submit();
};

document.body.appendChild(form);

submitToPopup(form);

, onsubmit , , , .

+3

[link],

  • URL-

    var form = document.createElement("form");
    
    form.setAttribute("method", "post");
    form.setAttribute("action", "http://www.yahoo.com");
    
    
    function popitup(url) {
        newwindow = window.open(url, 'name', 'width=800,height=600');
        if (window.focus) {
            newwindow.focus()
        }
        newwindow.document.body.appendChild(form);
        newwindow.document.forms[0].submit();
        return false;
    }
    
    popitup();
    
+1

, script , . , , : JSFiddle, .

jQuery:

$('.open-popup').click(function(e) {
    e.preventDefault();
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "");
    document.body.appendChild(form);
    form.submit();
    window.open(this.href, '_blank', 'width=300,height=200');
});

HTML:

<a href="about:blank" class="open-popup">Click Me</a>

, , . .

0

If you want to always keep the popup focused, you can use the trick originally found here . By quickly opening / closing / opening it, we guarantee that it always behaves like a new window and captures focus.

 <form name=f1 method=post action=test.html>

  <input type=text name=name value='123'>

  <input type='submit' value='Submit' 

  onclick="if(window.open('','reuse_win') != null) {
       window.open('','reuse_win').close();
    }; this.form.target='reuse_win'; return true;">

</form>

Also works with a link.

<a href="http://stackoverflow.com" onclick="
if(window.open('','reuse_win') != null) {
   window.open('','reuse_win').close();
}" target="reuse_win">Always opens in the same window and FOCUS</a>
0
source

All Articles