Results 1 to 7 of 7

Thread: How do I cancel a href in javascript?

  1. #1
    Join Date
    Nov 2009
    Posts
    42

    How do I cancel a href in javascript?

    Hello to all,
    I am using link like
    Code:
    <a href=go_somewheres.htm onClick=doTest() return>Go</a>
    and I have written JavaScript like:
    Code:
     function doTest(){
         return confirm("Click OK button to go to the next page, or click Cancel to stay on this page");
    }
    When I use this construct, it always go to the next page even if I press any button from the JavaScript pop-up. Can anyone tell me How do I cancel a href in javascript?
    Please help me.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How do I cancel a href in javascript?

    Code:
    <a href=go_somewheres.htm onClick=doTest() return>Go</a>
    Hey in your above code You haven't enclose value like "go_somewhere.htm" and function like "'return doTest();" in the inverted commas and that's why you are getting this problem. In this case you have to put these values into inverted commas to fix this problem. Use following code.
    Code:
    <a href='go_somewhere.htm' onClick='return doTest();'>

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How do I cancel a href in javascript?

    Do some changes in your html code like this. Instead of go_somewheres.htm use "#"
    Code:
    <a href="#" onClick=doTests();return false;>Go</a>
    Now write JavaScript like following:

    Code:
    function doTests(){
      var at =   return confirm("Click OK button to go to the next page, or click Cancel to stay on this page");
    if(at)
    window.location.href = "go_somewheres.htm"
    else
    return false;
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How do I cancel a href in javascript?

    As per my knowledge you have to remove return from the var line to get rid out of this problem. Just look at following example.

    Code:
    <BODY>
     <a href="#" onClick=doTests();return false;>Go</a>
    Use following JavaScript:
    Code:
    <script>
    function doTest(){
      var at =   confirm("Click OK button to go to the next page, or click Cancel to stay on this page");
    if(at)
    window.location.href = "http://www.google.com"
    else
    return false;
    }
    </script>
    </BODY>

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How do I cancel a href in javascript?

    Hey as per my knowledge you have to use following code to get rid out of this problem. In the following code I have given "return false;" to the onclick function.

    Code:
    <a href="http://www.gmail.com" onclick="return false;">Click here</a>
    Now you have to write javascript like this:
    Code:
    <script>
    function cancel(flags){
      if(flags == 1){
        return false;
      }else{
        return true; 
      }
    }
    </script>

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How do I cancel a href in javascript?

    As per my knowledge you have to use one of the following two html code to fix this problem. Using this code you will not able to go to the next page. Just try one of them in your code.
    Code:
    <a href="#" onclick="doSomethings(); return false;">
    Or
    Code:
    <a href="nonJavascriptAlternatives.html" id="foo">
    Now in the JavaScript write following:
    Code:
    $('foo').click(function() { doSomethings(); return false; });

  7. #7
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How do I cancel a href in javascript?

    I would recommended you to use following code in your program.
    Code:
    <a href="nons_javascripts_alternatives.html" onclick="doSomething(); return false;">
    In the above code instead of "go_somewheres.htm" I have use "nons_javascripts_alternatives.html". Using this you can either stay on the same page or you can go to the next page.

Similar Threads

  1. Set href value using javascript at onload
    By Jaganmohini in forum Software Development
    Replies: 4
    Last Post: 14-05-2010, 09:52 AM
  2. Why does onclick event not cancel href navigation?
    By Cordell in forum Software Development
    Replies: 5
    Last Post: 14-05-2010, 12:03 AM
  3. using href vs. onclick to call javascript function
    By Kalyug in forum Software Development
    Replies: 6
    Last Post: 12-05-2010, 10:09 AM
  4. Firefox Javascript Href issue
    By Lalitmohan in forum Software Development
    Replies: 5
    Last Post: 11-05-2010, 10:26 PM
  5. Cancel href via an onclick
    By Doshi1 in forum Software Development
    Replies: 5
    Last Post: 11-05-2010, 10:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,553,787.74821 seconds with 17 queries