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?
views:
31answers:
3
+2
A:
Like this:
window.onload = function(){
document.formName.submit();
};
Sarfraz
2010-07-17 20:32:06
A:
<form id=lol action="file.php"></form>
<script>document.getElementById('lol').submit();</script>
meder
2010-07-17 20:32:27
this has the added advantage of submitting the form before the page loads :)
Anurag
2010-07-17 20:39:15
however the original question specifically asked for form submit on page load, not before page load
bluesmoon
2010-07-18 02:18:53
+1
A:
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);
}
bluesmoon
2010-07-17 20:38:59