1

Below there is a function which allows to perform redirection method with some headers params. Everything works good but I want to open such redirection in a new browser tab (aka window open or target="_blank").

What should I change within below code ? Thanks!

function gotoUrl(path, params, method) {
    method = method || "post"; 

    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    //Fill the hidden form
    if (typeof params === 'string') {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", 'data');
        hiddenField.setAttribute("value", params);
        form.appendChild(hiddenField);
    }
    else {
        for (var key in params) {
            if (params.hasOwnProperty(key)) {
                var hiddenField = document.createElement("input");
                hiddenField.setAttribute("type", "hidden");
                hiddenField.setAttribute("name", key);
                if(typeof params[key] === 'object'){
                    hiddenField.setAttribute("value", JSON.stringify(params[key]));
                }
                else{
                    hiddenField.setAttribute("value", params[key]);
                }
                form.appendChild(hiddenField);
            }
        }
    }

    document.body.appendChild(form);
    form.submit();
}
ELing
  • 249
  • 6
  • 19
  • 1
    Possible duplicate of [How can I open a link in a new tab? Using JavaScript or jQuery. Without the browser alert of pop-up blocked](https://stackoverflow.com/questions/30479837/how-can-i-open-a-link-in-a-new-tab-using-javascript-or-jquery-without-the-brow) – rndus2r Sep 10 '17 at 07:32

0 Answers0