开发者

auto fill form and submit

开发者 https://www.devze.com 2023-02-09 03:08 出处:网络
I have a form on my 404 page that is auto filled with the address the user tried to find. I also have javascript that then auto submits that form.

I have a form on my 404 page that is auto filled with the address the user tried to find. I also have javascript that then auto submits that form.

The problem is, once it auto submits it keeps looping and the page keeps reloading.

I am trying to wright the javascript code to fire once and then stop. The script fires on page load so that's whats causing the loop.

Outcome: I need it to fire on page load, page reloads, the code checks to see if its alread开发者_开发问答y reloaded once then stops.

For my test I am trying to make it pop an alert that says "I reloaded once" just so I know its worked.

This is my code so far

<script type="text/javascript">
window.onload = function() { 
var grabedurl = window.location.href
document.getElementById('badurl').value=grabedurl;
if( history.previous != history.current ){alert('I reloaded once')}
else
setTimeout("document.getElementById('errorsubmit').click()", 3000);}
</script>


What you can do is add the state of the page already having been reloaded or not to the query string part of the URL. This should be done in your form's action, e.g. action="?submitted"

window.onload = function()
{ 
    var form = document.getElementById("aspnetForm");
    form.setAttribute("action", form.getAttribute("action") + "&submitted");
    var grabedurl = window.location.href;
    document.getElementById('badurl').value = grabedurl;
    if (/submitted/.test(window.location.search.substring(1)))
    {
        alert('I reloaded once');
    }
    else
    {
        setTimeout("document.getElementById('errorsubmit').click()", 3000);
    }
}

However, you might want to consider alternative approaches -- such as submitting the form via an XMLHttpRequest; having another, separate action page to submit the form to, or having the server log the request.

0

精彩评论

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

关注公众号