0

Below pasted code I use for posting data. The response opens in a new tab.

GF.SubmitForm = function (url, params, target) {
    // create form string using array push and join
    var form = ['<form method="POST" action="', url, '" target="', target, '">'];
    for (var key in params)
        form.push('<input type="hidden" name="', key, '" value="', params[key], '"/>');
    form.push('</form>');
    jQuery(form.join('')).appendTo('body')[0].submit();
}

What should I do to make this code open, the response data, in a new popup window.

Replaced the last line of code in the above function with the below pasted code but didn't work

 jQuery(form.join('')).appendTo('body')[0].submit(function () {
        window.open(url,target,"menubar=1,resizable=1,width=350,height=250");
    });
BumbleBee
  • 10,429
  • 20
  • 78
  • 123

1 Answers1

1

You won't be able to open a new window with the response from the POST using window.open(). Using Ajax is an option that might help you avoid side effects for other callers of GE.SubmitForm()

GF.SubmitForm = function (url, params, target) {
        if (target === 'newwindow')
        {
            var paramsString = '';
            for (var key in params)
                paramsString += key + '=' + params[key] + '&';
            var urlCopy = url;
            jQuery.ajax({
                cache: false,
                type: 'post',
                data: paramsString,
                dataType: 'html',
                url: urlCopy
            }).done(function(data){
                var newWindow = window.open(urlCopy,target,"menubar=1,resizable=1,width=350,height=250");
                newWindow.document.write(data);
            });
            return;
        }

        // create form string using array push and join
        var form = ['<form method="POST" action="', url, '" target="', target, '">'];
        for (var key in params)
            form.push('<input type="hidden" name="', key, '" value="', params[key], '"/>');
        form.push('</form>');
        jQuery(form.join('')).appendTo('body')[0].submit();
};

Forgive the magic string newwindow that would need to be the target value. You get the idea. You could also overload the function with a boolean for opening a new window, then call the original 3-arg function if false, and use the ajax if true.

Community
  • 1
  • 1
Tap
  • 6,332
  • 3
  • 22
  • 25