Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Confirmation when clicking a link

    Hi all, I want to have a javascript popup box so that when a hyperlink is clicked, a user is asked if they are sure they want to click this link, if they select ok then the page loads, if they click canel then nothing happens, so i have this code below:

    Code:
    <script type="text/javascript">
    function confirmSubmit()
    {
    var agree=confirm("Are you sure you wish to click this link?");
    if (agree)
    return true ;
    else
    return false ;
    }
    </script>
    Code:
    <a onclick="return confirmSubmit()" href="page.html">Page</a>
    However when a user clicks cancel the link still loads, can anyone see where I am going wrong? any help will be appreciated

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Code:
    function confirmSubmit(){
    	return confirm("Are you sure you wish to click this link?");
    }

  • #3
    New Coder
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Thank you for that, however it does not work and I still have the same problem

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Both your original code and Spudhead's condensed version are perfectly correct, and both work fine for me.

    But this sort of thing tends to annoy your visitors, who are not complete idiots. They are unlikely to click on the link by accident, and by clicking on a link show that they wish to leave your page.

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    I have had a mess around with some code and that method doesn't work for me either - the return value of the function has no effect on whether the link is followed.

    This works in FF 3.5, and probably could be modified to work in others:

    Code:
    window.addEventListener("load",convertlinks,false)
    
    var hrefs=[];
    var links;
    
    function convertlinks() {
    links=Array.prototype.slice.call(document.getElementsByTagName("a"));
    for(i in links) {
    hrefs[i]=links[i].href;
    links[i].removeAttribute("href");
    links[i].addEventListener("click",link_click,false);
    }
    }
    
    function link_click(e) {
    e=e.target||e.srcElement;
    if(confirm("yes?")) window.location.href=hrefs[links.indexOf(e)];
    }
    One thing I noticed is that without an href the link loses its default cursor and styling, which CSS will of course fix if you end up using something like this.
    Last edited by gusblake; 12-04-2009 at 07:45 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Sorry, but this works perfectly in all browsers:-

    Code:
    <script type="text/javascript">
    function confirmSubmit() {
    var agree=confirm("Are you sure you wish to click this link?");
    if (agree) {return true}
    else {return false}
    }
    </script>
    
    <a onclick="return confirmSubmit()" href="http://www.google.com">Page</a>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •