PreventDefault () method, preventdefault
PreventDefault () method
Event object
Definition and usage
Cancels the default action of an event.
Syntax
event.preventDefault()
Description
This method notifies the Web browser not to perform the default action associated with the event (if such an action exists ). For example, if the type attribute is "submit", you can call any event handle at any stage of event propagation. by calling this method, you can block form submission. Note: If the cancelable attribute of the Event object is fasle, no default action is available or the default action cannot be blocked. In either case, calling this method does not work.
The preventDefault method is used to prevent the occurrence of its default behavior from other things.
JS blocks link redirection
<Script type = "text/javascript">
| 12345678910111213141516 |
function stopDefault(e) { if (e && e.preventDefault) { e.preventDefault(); }else{ window.event.returnValue = false; } return false;}</script><a href="http://www.test.com" id="test"> Test </a><script type="text/javascript">var test = document.getElementByIdx_x('test');test.onclick = function(e) { alert('URL:' +this.href + ', Not redirected'); stopDefault(e);} |
</Script>
Click the link. The url is not opened. Only one dialog box is displayed.