Hi can someone please explain to me what is the difference between writing

onclick="confirmLink()"

and

onclick="return confirmLink()"

I tried both and they did the same thing and I am confused since the lazy fellows from my university have given me old notes in which they randomly include the "return" sometimes and sometimes they don't in the various examples.

<html>
  <head>
    <title>Handling onClick for links</title>
    <script>
      <!--
      function confirmLink()
      {
      alert("This is the Mastering JavaScript and JScript Home Page.");
      return confirm("Are you sure you want to load this document?")
      } 
      //-->
    </script>
  </head>
  <body>
  <h1>Handling onclick for links</h1>
  <p>
    <a href="http://www.jaworks.com/javascript" onclick="confirmLink()">Asks you
       to confirm your selection of this link.</a>
  </p>
  </body>
</html>

Recommended Answers

All 3 Replies

NO, both will behave differently if you click 'cancel' on confirmation dialog instead of clicking 'ok'.

Now its ur job to figure out something from this.

Try this example:

<html>
  <head>
    <title>Handling onClick for links</title>
    <script>
      <!--
      function confirmLink()
      {
      alert("This is the Mastering JavaScript and JScript Home Page.");
      return confirm("Are you sure you want to load this document?")
      } 
      //-->
    </script>
  </head>
  <body>
  <h1>Handling onclick for links</h1>
  <p>
    <a href="http://www.jaworks.com/javascript" onclick="">without retrun.</a><br>
    <a href="http://www.jaworks.com/javascript" onclick="return">with only retrun.</a><br>
    <a href="http://www.jaworks.com/javascript" onclick="return true;">with retrun true.</a><br>
    <a href="http://www.jaworks.com/javascript" onclick="return false">And with retrun false.</a><br>
  </p>
  </body>
</html>

Run this u will get all ur answers.

What I understand from this is that I can stop the default action from occuring if it return false!Thx.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.