简体   繁体   中英

Jquery link confirmation

I am implementing a form which has got links in it. like

<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?

Thanks in Advance.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM