Charanya
Charanya

Reputation: 41

jQuery link confirmation

I am implementing a form which has got links in it, like this:

<form>
<a>FAQ </a> /* something this way */
<submit button>
</form>

I have to display a confirmation box to the user, if the link is clicked and only when it tries to load the href. In case the user is opening the link in new tab or uses 'CMD-Click' (Mac), the prompt must not be shown. In Firefox, the browser itself takes care of this when the user tries to navigate to another page when he is in the middle of the form, but I need this functionality to work in all browsers.

Does anyone know how to do this?

Upvotes: 0

Views: 1123

Answers (2)

Uku Loskit
Uku Loskit

Reputation: 42040

Maybe something like this? demo

  <form>
        <a href="http://www.google.com">FAQ </a> /* something this way */
         <submit button>
  </form>




    $("a").click(function() {
        if (!confirm("Do you want to leave?")) {
            return false;
        }

    });

As for not displaying on new tab/new window, there are no such javascript events, thus you cannot capture them.

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39704

that's simple

<a href="http://www.google.com" onclick="return confirm('are you sure you want to go to Google?');">Google</a>

but i'm not sure if CMD+Click doesn't alert the user. Most of these event can't be controlled by javascript as they are coded into browsers.

Upvotes: 1

Related Questions