Toney
Toney

Reputation: 93

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

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 loaded without requiring any user action like pressing a button. How to do this?

Upvotes: 9

Views: 31353

Answers (3)

bluesmoon
bluesmoon

Reputation: 4320

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);
}

Upvotes: 2

meder omuraliev
meder omuraliev

Reputation: 186562

<form id=lol action="file.php"></form>
<script>document.getElementById('lol').submit();</script>

Upvotes: 18

Sarfraz
Sarfraz

Reputation: 382706

Like this:

window.onload = function(){
  document.formName.submit();
};

Upvotes: 11

Related Questions