JavaScript cancels browser default action
Default action refers to the operation performed by the browser that is not explicitly specified by the user. For certain HTML tags, the browser always has a default action.
http://www.baidu.com
Click the link above, and the browser will pop up a window to enter the Baidu homepage. This action is the default action of the browser: clicking an <a> tag will redirect you to the target page.
Other browser default actions include clicking the submit button to submit the form, clicking the reset button to reset the form, moving the mouse to an element with the title attribute to display a prompt, etc.
The browser's default action can be canceled through JavaScript.
For browsers that follow the W3C specification, use the preventDefault() method of the event object to cancel the default action; however, IE8.0 and below does not support this method. It assigns false to the returnValue attribute of the event object. to cancel the default action.
Cancel the default action of the <a> tag.
<html> <head> <title>取消<a>标签的默认动作</title> </head> <body> <a id="demo" href="http://www.baidu.com" target="_blank">点击这里试试</a> <script type="text/javascript"> document.getElementById("demo").onclick=function(e){ var eve = e || window.event; try{ // 使用 try...catch 语句避免浏览器出现错误提示 eve.preventDefault(); // 非 IE 浏览器 }catch(e){ eve.returnValue = false; // IE8.0 及其以下版本 } } </script> </body> </html>