1

I Want to submit a form to a popup window (not blank). This works fine but for some reason my parentwindow is opened for a 2nd time.

 var formelements = document.getElementById("exportform");
 formelements.removeAttribute("action");
 formelements.setAttribute("target","/path/exportwindow.html");
 exportwindow = window.open("/path/exportwindow.html", "myexportwindow", "width=800,height=600,resizable=yes");                                         

 formelements.submit();

HTML:

<form id="exportform" action="/path/myfunction" method="post">

So what happens is that the popup window opens and in the background my parent window is opened to a new tab. Can anyone tell me why js is behaving like this?

nh_
  • 299
  • 5
  • 25
  • we can't understand please describe it on stack overflow new feature snippet – Man Programmer Sep 23 '14 at 09:25
  • Possible duplicate of [Window.open and pass parameters by post method](http://stackoverflow.com/questions/3951768/window-open-and-pass-parameters-by-post-method) – Ulad Kasach May 18 '16 at 19:31

1 Answers1

2

The second parameter in window.open is the window name, and that must be the same as target

 var formelements = document.getElementById("exportform");
 formelements.setAttribute("action","path/to/exportwindow.php");
 formelements.setAttribute("target","bugsme");
 exportwindow = window.open("", "bugsme", "width=800,height=600,resizable=yes");

 formelements.submit();

This opens a single popup, but you should probably not remove the action parameter but set it to the correct script receiving the form data

FrancescoMM
  • 2,845
  • 1
  • 18
  • 29
  • Yes like this it submits to a new window, this one looks the same as the parent, but I want it to submit to a new window with a new html in it, what is exportwindow.html. Is there a way to do that? – nh_ Sep 23 '14 at 09:11
  • @user3891252 Set the the action parameter to the correct script (the one that receives your form data). Also an html can do "/path/exportwindow.html" even if that is unusual (a form with nothing inside) – FrancescoMM Sep 23 '14 at 09:14
  • 1
    @FrancescoMM,, very useful and save my time. Thank you very much. One more question, why cann't remove the action parameter form setAttribute method? Like exportwindow = window.open("path/to/exportwindow.php", "bugsme", "width=800,height=600,resizable=yes"); – T8Z Nov 20 '15 at 04:04
  • 1
    @T8Z If you do, it will open a popup calling exportwindow.php with no parameters (no form data) and then do the submit sending the data to the action defined in HTML (or current script). A submit with correct target (=the window) and action (=the script) parameters instead will pass all the form data correctly to the right script, and show the result in the right window. If you want to remove the action and target parameters you must put them in the HTML, in the FORM tag, not in the window.open call. – FrancescoMM Nov 21 '15 at 08:40