Why does onclick event not cancel href navigation?

As far as I understand the HTML code:

    <a onclick="return confirm('Are you sure?');" href="http://news.bbc.co.uk">BBC</a>

should produce a HTML page with a single link that if clicked asks "Are you sure" and then if you press Cancel the link will not fire. However, it goes to the link whether I press OK or Cancel.

Why does it do this and how can I get the click to cancel?
Who is Participating?
I copied exactly what you posted and it works as expected. There's nothing wrong with it. Make sure your page doesn't have a typo.
BOITAuthor Commented:
Thank you, you are correct, the code if fine.

the problem was with the browser. I had javascript enabled - the confirm message was displaying - but the navigation occured whichever button was pressed.

I did a full reset on Internet Explorer - lost all saved passwords - the lot. The code now works correctly.

Thanks for pointing me in the right direction - I was pulling my hair out!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.