[자바스크립트] 새창으로 POST 데이터 전송

여러 방법이 있지만 간단한 함수를 찾았기에 기록해 둔다.

출처: https://stackoverflow.com/questions/5684303/javascript-window-open-pass-values-using-post

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);
}

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

Leave a Comment