0

I'm trying to trigger a click event on a link from within a click handler for the same link. I want to stop the link from navigating until the user has been notified of some action. Once the action is complete the link should navigate as normal.

Please see the test case below.

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
        <script language="javascript" type="text/javascript">
            $(function () {
                $("#test").click(function (e) {
                    var lnk = $(this);

                    if (lnk.data("checked") != true) {

                        var notice = $('<a href="#">Continue</a>');
                        $('body').append(notice);

                        notice.click(function (e) {
                            lnk.data("checked", true);
                            lnk.click();

                            return false;
                        });

                        return false;
                    }

                    alert("should navigate now");
                });
            });
        </script>

        <a id="test" href="http://www.google.com">Test</a>

    </body>
</html>

The expected behavior for this example is;

  • Test clicked
  • Continue appears
  • Continue clicked
  • Page navigates to the href of 'Test'

Instead, when continue is clicked, the alert 'should navigate now' is shown but the browser doesn't perform any action.

4
  • 1
    manual triggering of a click event may not trigger the default behavior of the element Commented Feb 13, 2014 at 13:23
  • Check type of lnk.data("checked"), it could be considered not falsy Commented Feb 13, 2014 at 13:23
  • @ArunPJohny do you have a reference for that? Commented Feb 13, 2014 at 13:25
  • @u_mulder that's why I check != true which covers null, "", undefined, false or anything except true really :) Commented Feb 13, 2014 at 13:27

4 Answers 4

1

Try triggering the click using the dom element

   notice.click(function (e) {
       lnk.data("checked", true);
       lnk[0].click();
       // ^^^^
       return false;
   });

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

This works. Do you know why? Presumably jquery is interfering, does it have some sort of 'potential infinite loop' check?
@Alex I'm not exactly sure why jQuery is not allowing the default behavior of a link, but i think it's intended.
@Alex found some info here stackoverflow.com/a/5867423/1696560 You can only trigger a click that jQuery has created. It's one of jQuery's cute little quirks.
0

Just add document.location.href = lnk.attr('href'); after alert.

I think, the problem appears, because alert breaks callback.

Comments

0

Not sure this is what you want but try to rewrite your code like this:

$(function () {
    $("#test").click(function (e) {
        e.preventDefault();
        if ($(this).data("checked") != true) {
            var notice = $('<a class="link" href="#">Continue</a>');
            $('body').append(notice);
        }
        alert("should navigate now");
    });

    $(document).on('click', '.link', function (e) {
        $("#test").data("checked", true);
        window.location = $("#test").attr('href');   
    });
});

Comments

0

I know this is old, but I want to contribute to those that find this question and answers from now on.

If you're trying to confirm whether a user wants to leave a site, then consider the window.onbeforeunload event handler. You are able to put in a string message that is presented to the user in an alert-like message box.

<script>
window.onbeforeunload = function (e) {
    e = e || window.event;

    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue = 'Sure?';
    }

    // For Safari
    return 'Sure?';
};
</script>

Check the first answer here: Confirmation before closing of tab/browser

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.