My website needs to have a form that automatically submits, sending the user (with POST data) to an external website. Previously, I was using HTML like this:
<html>
<head></head>
<body onload="document.forms[0].submit()">
<form name='form1' method='post' action='externalwebsite.com'>
<input type='hidden' name='cartId' value='1337'>
<input type='hidden' name='currency' value='USD'>
<input type='hidden' name='amount' value='9'>
</form>
</body>
</html>
However, this resulted in a long delay between the page loading and the form being submitted, as the onload event fires very late in the page lifecycle.
Javascript libraries have the domready event, to allow code to before the onload event fires, but still at a point where it is safe to manipulate the DOM tree. For instance, with the Mootools library, I could do this:
window.addEvent('domready', function () {
document.forms[0].submit();
});
However, I don’t want to include a bulky javascript library for such a simple page. As such, I have the following HTML:
<html>
<head></head>
<body>
<form name='form1' method='post' action='externalwebsite.com'>
<input type='hidden' name='cartId' value='1337'>
<input type='hidden' name='currency' value='USD'>
<input type='hidden' name='amount' value='9'>
</form>
<script type='text/javascript'>document.forms[0].submit()</script>
</body>
</html>
Will this always work, across all browsers? Or should I use a different method?
put the script before closing body tag