Results 1 to 6 of 6

Thread: Why does onclick event not cancel href navigation?

  1. #1
    Join Date
    Dec 2009
    Posts
    33

    Why does onclick event not cancel href navigation?

    Hello to all,
    I have written following HTML code:
    Code:
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <a onclick="return confirm('Are you sure?');" href="http://www.google.com">BBC</a>
    </body>
    </html>
    I want to produce HTML page with a single link. If I click on that link, it will ask for confirmation and If I press Cancel the link will not activate. But using above code it goes to the other link even if I press cancel. Can anyone tell me why does onclick event not cancel href navigation?
    Please help me.

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

    Re: Why does onclick event not cancel href navigation?

    Hey I exactly copied your code and paste it in notepad. After that I make html page of it and it is working fine with me. It works as expected. I haven't get any problem with it. When I click on the cancel button, nothing happened and When I click on the OK button, it goes to the next link. I think you haven't refresh you page after making changes in the notepad and that's why you are getting such type of problem. Just try to refresh you browser page and then again try it.

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

    Re: Why does onclick event not cancel href navigation?

    From your information it seems that you are using Opera and that's why you are getting this problem. when you use Opera browser to make such type of validation and calculation, it does not work properly and that's why you didn't get proper result. In this case you have to use another browser like Firefox and Internet explorer.

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

    Re: Why does onclick event not cancel href navigation?

    Code:
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <a onclick="return confirm('Are you sure?');" href="http://www.google.com">google</a>
    </body>
    </html>
    Hey you have to use above code to get rid out of this problem. In the above code I have assign "return confirm('Are you sure?');" function to the onclick variable. Just make these changes and try to run again.

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

    Re: Why does onclick event not cancel href navigation?

    First use following HTML code
    Code:
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <a href="http://www.google.com"  onclick="return confirm('Are you sure?');">google</a>
    </body>
    </html>
    In the above code I have assign "return confirm('Are you sure?');" function to the onclick event.
    Now write javascript like this;
    Code:
    <script LANGUAGE="javascript" >
    <script>

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

    Re: Why does onclick event not cancel href navigation?

    Quote Originally Posted by Cordell View Post
    Hello to all,
    I have written following HTML code:
    Code:
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <a onclick="return confirm('Are you sure?');" href="http://www.google.com">BBC</a>
    </body>
    </html>
    I want to produce HTML page with a single link. If I click on that link, it will ask for confirmation and If I press Cancel the link will not activate. But using above code it goes to the other link even if I press cancel. Can anyone tell me why does onclick event not cancel href navigation?
    Please help me.
    Hey there is no need to worry about it. Just save that code in to another notepad and save it with .html extension. Now try to open that page with another browser.

Similar Threads

  1. IE7 href + onclick - both happen
    By Ucchal in forum Software Development
    Replies: 5
    Last Post: 24-05-2010, 03:58 PM
  2. CSS Button onclick event href="#" onclick="
    By Cornnelius in forum Software Development
    Replies: 6
    Last Post: 14-05-2010, 11:20 PM
  3. Canceling href navigation during onclick event
    By Cordell in forum Software Development
    Replies: 6
    Last Post: 13-05-2010, 11:56 PM
  4. A Href And OnClick Button
    By Endowed in forum Software Development
    Replies: 5
    Last Post: 11-05-2010, 10:24 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,569,344.59138 seconds with 17 queries