自动填写表格并提交

发布于 2024-10-16 15:27:09 字数 630 浏览 3 评论 0原文

我的 404 页面上有一个表单,它会自动填写用户尝试查找的地址。我还有 javascript,然后自动提交该表单。

问题是,一旦自动提交,它就会不断循环并且页面不断重新加载。

我正在尝试将 javascript 代码编写为触发一次然后停止。 该脚本在页面加载时触发,这就是导致循环的原因。

结果:我需要它在页面加载、页面重新加载时触发,代码检查它是否已经重新加载一次然后停止。

对于我的测试,我试图让它弹出一个警报,提示“我重新加载了一次”,这样我就知道它有效。

这是我到目前为止的代码

<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>

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 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2024-10-23 15:27:09

您可以做的是将页面是否已重新加载的状态添加到 URL 的查询字符串部分。这应该在您的 formaction 中完成,例如 action="?subscribed"

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

但是,您可能需要考虑其他方法 --例如通过 XMLHttpRequest 提交表单;有另一个单独的 action 页面来提交表单,或者让服务器记录请求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文