Search code examples
javascriptwindow.opencreateelement

window.open createElement('form') with submit input type is not calling the action


I am trying to open a new window with a custom form in javascript

In the form I am having some fields and action along with the submit input (please note that the element should be of input type=submit

However, when clicking the submit button in the popup window, the submit action is not performed.

    var map = window.open("","editormap",'width=600,height=600,scrollbars=no,resizable=no');
    
        var mapForm = map.document.createElement('form');
        mapForm.target = "editormap";
        mapForm.id="inpForm";
        mapForm.method = "POST"; 
        mapForm.action = "/dashboard/save/"+val1+"/"+val2;
    
      var mapInput3 = map.document.createElement('input');
      mapInput3.type = 'submit';
      mapInput3.name = 'submit';
      mapInput3.value = 'Submit';
      mapForm.appendChild(mapInput3);
    
       var  mapInput = map.document.createElement('input');
        mapInput.type = "button";
        mapInput.name = "cancel";
        mapInput.value = "Cancel";
        mapInput.onclick=map.close;
        mapForm.appendChild(mapInput);
        
    
      var  mapInput1 = map.document.createElement("textarea");
        mapInput1.name = "code";
        mapInput1.value = repoValues;
        mapForm.appendChild(mapInput1);
        
     var mapInput2 = map.document.createElement("input");
     mapInput2.type = "hidden";
    mapInput2.name = "filename";
    mapInput2.value = 'some data';
    mapForm.appendChild(mapInput2);
    
   map.document.body.appendChild(mapForm); 

I have also tried bind like below

var parentForm = document.createElement.bind(document);
var mapForm = parentForm('form');

still the submit is not performing the requested action.

Please help or guide in the right direction.


Solution

  • Since the window has an empty location, relative URLs have no base URL to be interpreted relative to. So you need to provide the full URL in the action URL.

            mapForm.action = "https://www.example.com/dashboard/save/"+val1+"/"+val2;
    

    DEMO

    (Stack Snippets don't allow popup windows, so I had to use jsfiddle for the demo.)