简体   繁体   English

自动填写表格并提交

[英]auto fill form and submit

I have a form on my 404 page that is auto filled with the address the user tried to find. 我的404页上有一个表单,该表单会自动填充用户尝试查找的地址。 I also have javascript that then auto submits that form. 我也有JavaScript,然后自动提交该表格。

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. 我正在尝试使javascript代码触发一次然后停止。 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 already 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. 您可以做的是将已经重新加载的页面状态添加到URL的查询字符串部分中。 This should be done in your form 's action , eg action="?submitted" 这应该在formaction ,例如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; 但是,您可能需要考虑其他方法-例如通过XMLHttpRequest提交表单; having another, separate action page to submit the form to, or having the server log the request. 拥有另一个单独的action页面来提交表单,或者让服务器记录请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM