How to cause a form to be submitted automatically on page load in JavaScript?

前端 未结 3 1536
死守一世寂寞
死守一世寂寞 2020-12-19 03:36

This may have an obvious answer, but I am very new to js so I have no idea how to do this. I would like a form on a page to be submitted automatically when that page is load

相关标签:
3条回答
  • 2020-12-19 03:41

    I'd go with what sAc suggests, but I'd use an event listener rather than just writing to window.onload:

    function submit_form() { document.formName.submit(); }
    
    if(window.attachEvent){
        window.attachEvent("onload", submit_form);
    }else{
        window.addEventListener("load", submit_form, false);
    }
    
    0 讨论(0)
  • 2020-12-19 03:50

    Like this:

    window.onload = function(){
      document.formName.submit();
    };
    
    0 讨论(0)
  • 2020-12-19 03:57
    <form id=lol action="file.php"></form>
    <script>document.getElementById('lol').submit();</script>
    
    0 讨论(0)
提交回复
热议问题