JavaScript

Post Parameters in Window.open / Window.location.href (Alternative)

In some cases we have a requirement, to POST data i.e. you’re accessing the URL can’t see the parameters passed, as some information might be sensitive so we can’t pass POST parameters in URL, but there is an alternative way to post parameters.

Let’s see the Example –
The below method is an alternative to Window.open or Window.location.href, if you want to POST the parameters.

JavaScript Method:-

var openPost = function (url, params) {
    var formElement = document.createElement("form");
    formElement.setAttribute("method", "post");
    formElement.setAttribute("action", url);
    formElement.setAttribute("target", "_parent");

    for (param in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("name", param);
        hiddenField.setAttribute("value", params[param]);
        formElement.appendChild(hiddenField);
    }

    document.body.appendChild(formElement);
    formElement.submit();
}

Calling Method to Redirect -
// CALLING THE OPENPOST METHOD TO REDIRECT
var params = [];                            
params["token"] = “xyz112121212xysy”;
                               
openPost('/your/redirectURL', params);

//END OPEN POST METHOD
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Post Parameters in Window.open / Window.location.href (Alternative) Post Parameters in Window.open / Window.location.href (Alternative) Reviewed by Anil Singh on 10:09 PM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^