[英]Auto go back after clicking a link in Javascript
When a user click an href link with id="boo" I am trying to get the browser to automatically go 'back' after 2 seconds.当用户单击 id="boo" 的 href 链接时,我试图让浏览器在 2 秒后自动 go '返回'。
This isn't working and i'm reaching out for some help from you all.这不起作用,我正在向大家寻求帮助。 Thanks in advance!
提前致谢!
document.getElementById("boo").addEventListener("click",
function a(event) {
setTimeout(function() {window.location = History.back},2000)}
);
Edit: (Adding this to clarify my goal) so basically i want to do two things on click.编辑:(添加这个以澄清我的目标)所以基本上我想在点击时做两件事。 navigate to a different page (time.is) and then automatically go back to the original page after 4 seconds.
导航到不同的页面 (time.is),然后在 4 秒后自动 go 回到原始页面。
I think you have to make the default function of <a>
tag stop working.我认为您必须使
<a>
标签的默认 function 停止工作。 Please try adding .preventDefault()
.请尝试添加
.preventDefault()
。
document.getElementById("boo").addEventListener("click",
function (event) {
event.preventDefault();
setTimeout(function() {window.history.back()}, 2000);
}
);
If you want to do two things on one click, I suggest this:如果你想一键做两件事,我建议这样做:
JavaScript: JavaScript:
document.getElementById("boo").addEventListener("click",
function (event) {
// Remove .preventDefault()
setTimeout(function() {window.history.back()}, 2000);
}
);
HTML: HTML:
<a id="boo" class="clock" href="https://time.is/" target="_blank"></a>
A part from a syntax error ( history
should be lower case), you are setting window.location with the back function, without executing it.语法错误的一部分(
history
应该是小写),您正在设置 window.location 与后面的 function,而不执行它。
This should fix your code:这应该修复您的代码:
document.getElementById("boo").addEventListener("click",
function(event) {
setTimeout(function() {window.history.back()}, 2000);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.