1

I have this html where I need to make payment to payment gateway.

<html>
<head>​</head>​

<body>
  <form id="pay_form" method="post">
    <div className="text-center">
      <div style="text-align:center;max-width:1170px;margin:0 auto;"></div>
      <input type="hidden" name="MerchantId" id="MerchantId"/>

    </div>
  </form>
</body>
<script>
    CheckOut();
        function CheckOut() {
      document.getElementById("pay_form").action = "<%= requestUrl %>";
            document.getElementById("MerchantId").value = "<%= merchantId %>";
      document.getElementById("pay_form").submit();
        }
</script>
</html>

If I do window.open('myhtml.html'); the html will execute the script and submit the form but how to bind the MerchantId value to it before that?

Andre Thonas
  • 231
  • 1
  • 9

1 Answers1

0

You can use URL query for that, like so:

const MerchantId = "<%= merchantId %>";
window.open(`myhtml.html?MerchantId=${MerchantId}`);

And then in your myhtml.html:

const MerchantId = getQueryVariable('MerchantId');

// credits: https://stackoverflow.com/a/2091331/4536543
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=');
        if (decodeURIComponent(pair[0]) == variable) {
            return decodeURIComponent(pair[1]);
        }
    }
    console.log('Query variable %s not found', variable);
}
Maxim Mazurok
  • 2,730
  • 1
  • 16
  • 29
  • I can't reveal it to client, I have to use js to post it to backend. – Andre Thonas Apr 24 '20 at 09:46
  • Please, describe what you're doing, what you're trying to achieve and what is not working. You want to submit the form and then open the window? How you do it and what is not working? – Maxim Mazurok Apr 24 '20 at 11:13