Block a link from working using preventDefault() - Javascript DOM Event

Javascript examples for DOM Event:preventDefault

Description

Block a link from working using preventDefault()

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <a id="link" onclick="test()" href="http://www.example.com">Text</a> 
      <script type="text/javascript">
function test() {/*from w  ww .j  a v  a 2s  .c o m*/
  return "texttoblock" === prompt("Some question / text").toLowerCase() ? (window.event.preventDefault(), console.log("text"), !1) : !0
}
      </script>  
   </body>
</html>

Related Tutorials