SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Confirming an <A>

    Ok this is what I want to do

    I have a link
    <A HREF="blah">Delete</A>

    when I click on it I want a confirm msg to pop-up and if the user presses NO then it won't load the page if YES then it will delete.

    I tried to do this but I don't know enough about JS yet so I gave up. Plus it's 2:00am here.

  2. #2
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    <a href="http://www.google.com/" onClick="return confirm('Are you sure you want to do this?');">click</a>

  3. #3
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is an example that works for me:
    <html>
    <head>
    <title>Link Confirm</title>
    <SCRIPT LANGUAGE="JavaScript">
    function linkto(linkid)
    {
    if(confirm("Are you sure?"))
    {location.href=linkid}
    }
    </SCRIPT>
    </head>
    <body>
    <BR><BR>
    <CENTER><A HREF="#" onClick="linkto('page1.html')">Click Me</A></CENTER>
    </body>
    </html>

  4. #4
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... that code will work just fine but it will not function in browsers that have javascript disabled (or never had javascript support in the first place). I prefer to keep the page to link to in the href="" part of the tag and use an onClick event to provide the dialog - that way users with JS disabled will still be able to click on the link.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I need that to confim a delete action for members only. And since this is a clan site only a fraction of the visitors can access this. And I'm pretty sure most of these members (if not all have JS enabled). But I like skunk's method better since it's simple and all.
    But Request.. I like your method too because now I know how to do this 2 ways.

    Thank you

  6. #6
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If they have Javascript disabled then the code provided by Skunk also will not work since it uses javascript. The only difference is you have placed your javascript inline and I have placed mine in a function. I will say one thing though - my version will not work correctly with IE5.01 version browsers. There is a bug when you use the void character "#" so it should be changed to this:
    <CENTER><A HREF="javascript:linkto('page1.html')">Click Me</A></CENTER>

  7. #7
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by requestcode
    If they have Javascript disabled then the code provided by Skunk also will not work since it uses javascript. The only difference is you have placed your javascript inline and I have placed mine in a function.
    You are missing a major difference between your script and his, request.

    If a non-javascript user clicks on a link using Skunk's script then the javascript will be ignored, but the link will still function.
    All that will happen is there won't be any confirm.

    If the same user clicks on a link using your script then absolutely nothing will happen. The link is dead.
    All your url info is within the javascript, so without javascript the browser cannot get anything useful from that <a> link.
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  8. #8
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes Bill I did understand his point, but the original question was to have a confirm msg so javascript is the only way to do this. I don't disagree with you or Skunk, but I am of the opinion that most people do not know how to disable Javascript. For those that have older browsers that don't support it they should upgrade. Most sites now employ some JavaScript to make them more interactive, easier to navigate or to perform some other function. To disable JavaScript will make it very difficult for them get much use out of those sites.

  9. #9
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either way is good. Don't get into it too much. Just Skunk's is simpler. I would use the function only if I'd be doing more when the link was clicked.


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
  •