Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 69587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:35:50+00:00 2026-05-10T19:35:50+00:00

A pattern that’s started to show up a lot in one of the web

  • 0

A pattern that’s started to show up a lot in one of the web apps I’m working are links that used to just be a regular a-tag link now need a popup box asking ‘are you sure?’ before the link will go. (If the user hits cancel, nothing happens.)

We’ve got a solution that works, but somehow we’re a web app shop without a Javascript expert, so I’m left with this crawling feeling like there’s a better way to get the job done.

So, JS experts, what’s the most standards-compliant, cross-browser way to get this done?

(For the record, this is already a site that requires JS, so no need to have a ‘non-JS’ version. But, it does need to work in any and all reasonably modern browsers.)

(Also, for bonus points it would be nice if people with JS turned off didn’t have the links work, rather than bypassing the confirm box.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:35:51+00:00Added an answer on May 10, 2026 at 7:35 pm

    Unobtrusive Javascript

    The best practice is to add event handler methods to the links.

    The confirm() function produces the dialog box you described, and returns true or false depending on the user’s choice.

    Event handler methods on links have a special behavior, which is that they kill the link action if they return false.

    var link = document.getElementById('confirmToFollow');  link.onclick = function () {     return confirm('Are you sure?'); }; 

    If you want the link to require javascript, the HTML must be edited. Remove the href:

    <a href='#' id='confirmToFollow'... 

    You can explicitly set the link destination in the event handler:

    var link = document.getElementById('confirmToFollow');  link.onclick = function () {     if( confirm('Are you sure?') ) {         window.location = 'http://www.stackoverflow.com/';     }     return false; }; 

    If you want the same method called on multiple links, you can acquire a nodeList of the links you want, and apply the method to each as you loop through the nodeList:

    var allLinks = document.getElementsByTagName('a'); for (var i=0; i < allLinks.length; i++) {     allLinks[i].onclick = function () {         return confirm('Are you sure?');     }; } 

    There are further permutations of the same idea here, such as using a classname to determine which links will listen for the method, and to pass a unique location into each based on some other criteria. They are six of one, half dozen of another.

    Alternative Approaches (not encouraged practices):

    One discouraged practice is to attach a method via an onclick attribute:

    <a href='mypage.html' onclick='... 

    Another discouraged practice is to set the href attribute to a function call:

    <a href='javascript: confirmLink() ... 

    Note that these discouraged practices are all working solutions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Map Reduce is a pattern that seems to get a lot of traction lately
The gridExtra package adds a grob of class pattern that lets one fill rectangles
So, one problem pattern that I keep coming across and don't have a good
I've noticed a pattern that can make refactoring MVC2 apps difficult. When you change
Can anyone suggest a pattern that can be used for writing a JavaScript API
I need a pattern that will traverse the document and get me all links
I created a regex pattern that works perfect, but I can't get it working
The singleton pattern is a design pattern that is used to restrict instantiation of
It is a pattern that occurs quite often in one part of our Framework.
I'm working with a pattern that looks like so (pseudo example): var FOO =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.