0

Additional requirements:

  • Make it generic
  • Make it open in a new tab
  • Make it open in a new tab with IE11 (tougher still)
  • Make it post to an external/different domain
rothschild86
  • 1,433
  • 16
  • 22
  • SO allows Question & Answer type of questions. Try asking a new question and you will see a special checkbox towards the bottom enabling this functionality. See this - https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/ – rothschild86 Jun 07 '17 at 14:32
  • yes! but then the answer should be really good, and it shouldnt be a duplicate of https://stackoverflow.com/questions/19064352/how-to-redirect-through-post-method-using-javascript – Jonas Wilms Jun 07 '17 at 14:37
  • none of those solutions will open in a new tab in IE11. See this discussion - https://social.msdn.microsoft.com/Forums/ie/en-US/a5c294e2-e407-491d-ba6a-b7f7edbcabaf/ie11-cant-post-form-data-to-specific-frame-or-window-dialog-opened-via-windowopen?forum=iewebdevelopment – rothschild86 Jun 07 '17 at 14:43

1 Answers1

0

The issue is that we cannot POST into a named tab cross domains in IE11.

Instead let's create a file that will build out a form using the query string params.

Now it's just a matter of issuing a get request to that file.

First, create a file called PostRedirect.htm with the following code:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript" src="HTMLClient/Scripts/jquery-2.2.4.min.js"></script>
    <script type="text/javascript">

        //This adapter file allows POSTing to any url via a GET
        //Main advantage of this method is that it is IE11 compatible (posting to new named tab is no longer possible in IE11 w/o adjusting the security policy)

        //Must pass a param called url

        $(document).ready(function () {

            //get all the params from the query string
            var urlParams;
            (window.onpopstate = function () {
                var match,
                    pl = /\+/g,  // Regex for replacing addition symbol with a space
                    search = /([^&=]+)=?([^&]*)/g,
                    decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
                    query = window.location.search.substring(1);

                urlParams = {};
                while (match = search.exec(query))
                    urlParams[decode(match[1])] = decode(match[2]);
            })();

            var postLaunch = function (url, params) {

                var form = document.createElement("form");
                form.setAttribute("method", "post");
                form.setAttribute("action", url);
                for (var i in params) {
                    if (params.hasOwnProperty(i)
                        && i != 'url' //no need to submit the url param itself
                        ) {
                        var input = document.createElement('input');
                        input.type = 'hidden';
                        input.name = i;
                        input.value = params[i];
                        form.appendChild(input);
                    }
                }
                document.body.appendChild(form);
                form.submit();               
            };

            postLaunch(urlParams['url'], urlParams);
        });
    </script>
</body>
</html>

Now you simply call the above file with the params of your choice like so:

    var params = {
        url: 'http://oris.co.palm-beach.fl.us/or_web1/new_sch.asp',
        search_by: 'Official Book/Page',
        RecSetSize: 100,
        PageSize: 20,
        search_entry: '24799-0613'
    };
    window.open('/postredirect.htm?' + $.param(params), '_blank');
rothschild86
  • 1,433
  • 16
  • 22