Javascript window.open pass values using POST Javascript window.open pass values using POST javascript javascript

Javascript window.open pass values using POST


I used a variation of the above but instead of printing html I built a form and submitted it to the 3rd party url:

    var mapForm = document.createElement("form");    mapForm.target = "Map";    mapForm.method = "POST"; // or "post" if appropriate    mapForm.action = "http://www.url.com/map.php";    var mapInput = document.createElement("input");    mapInput.type = "text";    mapInput.name = "addrs";    mapInput.value = data;    mapForm.appendChild(mapInput);    document.body.appendChild(mapForm);    map = window.open("", "Map", "status=0,title=0,height=600,width=800,scrollbars=1");if (map) {    mapForm.submit();} else {    alert('You must allow popups for this map to work.');}


Thank you php-b-grader. I improved the code, it is not necessary to use window.open(), the target is already specified in the form.

// Create a formvar mapForm = document.createElement("form");mapForm.target = "_blank";    mapForm.method = "POST";mapForm.action = "abmCatalogs.ftl";// Create an inputvar mapInput = document.createElement("input");mapInput.type = "text";mapInput.name = "variable";mapInput.value = "lalalalala";// Add the input to the formmapForm.appendChild(mapInput);// Add the form to domdocument.body.appendChild(mapForm);// Just submitmapForm.submit();

for target options --> w3schools - Target


For what it's worth, here's the previously provided code encapsulated within a function.

openWindowWithPost("http://www.example.com/index.php", {    p: "view.map",    coords: encodeURIComponent(coords)});

Function definition:

function openWindowWithPost(url, data) {    var form = document.createElement("form");    form.target = "_blank";    form.method = "POST";    form.action = url;    form.style.display = "none";    for (var key in data) {        var input = document.createElement("input");        input.type = "hidden";        input.name = key;        input.value = data[key];        form.appendChild(input);    }    document.body.appendChild(form);    form.submit();    document.body.removeChild(form);}