自动填写表格并提交
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做的是将页面是否已重新加载的状态添加到 URL 的查询字符串部分。这应该在您的
form
的action
中完成,例如action="?subscribed"
但是,您可能需要考虑其他方法 --例如通过 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
'saction
, e.g.action="?submitted"
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.