PreventDefault () method, preventdefault

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.