开发者

Best way to form submit using Javascript

开发者 https://www.devze.com 2023-04-12 02:11 出处:网络
I have two methods for this - <body onLoad=\"document.frmconfirm.submit();\"> <form name=\"frmconfirm\" action=\"https://abc.com/\" method=\"post\">

I have two methods for this -

<body onLoad="document.frmconfirm.submit();">
  <form name="frmconfirm" action="https://abc.com/" method="post">
    <input type='hidden' name='account_id' value='5715' />
  </form>
</body>

and

<form id='subFrm' name='subFrm' method='post' action='https://abc.com'>
   <input type='hidden' name='msg' value='dummy'>
</form>
<script&开发者_JS百科gt;document.subFrm.submit();</script>

I am getting some issue on firefox in second one.

I would love to hear your thoughts and any other full proof approach.

I need to automate this process on a page, so can't use jQuery.


What are "some issues"?

Your two snippets of html/javascript should be pretty much identical in their functionality. The first is probably preferable in any case as the javascript runs when the page fully loads. The second runs some javascript directly as it is parsed, which should work fine as long as, as in your second example, the form has already been parsed.

TL;DR - They're (pretty much) the same.


Try

<form id='subFrm' name='subFrm' method='post' action='https://abc.com'>
   <input type='hidden' name='msg' value='dummy'>
</form>
<script>document.getElementById("subFrm").submit();</script>

or

<form id='subFrm' name='subFrm' method='post' action='https://abc.com'>
   <input type='hidden' name='msg' value='dummy'>
</form>
<script>window.onload = new function() { document.getElementById("subFrm").submit() };</script>

You could use jQuery to do this btw :)

$(document).ready(function () {
    $("#subFrm").submit()
}
0

精彩评论

暂无评论...
验证码 换一张
取 消