2

As stated in the title I'm having some issues with my JS.

js:

$("#bg2InsideFb").click(function () {
       $("#fbLink").click();
    });

html:

<div id="bg2InsideFb">
    <p id="fbIcon">
        <img src="@routes.Assets.at("images/mobile/login/fblogin-ico.png")" alt="Facebook"/>
    </p>
    <p class="pl20">
        <a id="fbLink" href="@fb.authenticationUrl"> @Messages("review.form.facebook.login") </a>
    </p>
 </div>
 <p class="floatleft f13">
    @Messages("review.form.facebook.term")
 </p>

This results Chrome to crash and Firefox to throw a too much recursion error. What is happening and how do I solve this?

5 Answers 5

4

You are programmatically clicking #fbLink which is an element within #bg2InsideFb. Event bubbling causes that very same click to effect the later element which then triggers another click. To prevent this, you'll need to hook a listener to the inner element which prevent propagation like this:

$('#fbLink').click(function(jqEvt) {
    jqEvt.stopPropagation();
}

See the jQuery API on event.stopPropagation.

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

7 Comments

Hmm I added your code, no error now, but nothing happens. I guess I missed something?
@jakob I don't even know what it's supposed to do. Since it is a link, I assume new content ought to be loaded. What is your new code?
Yes you are correct. The new content should be loaded. The code is the same as before I just added the stopPropagation code to js file.
@jakob There shouldn't be any issue with stopPropagation. I'd assume the error to lie somewhere else. Is it an asynchronous request or have you registered more click handlers on anchors?
Hmm a regular syncronous request to antother page on the same domain If I do $('#fbLink').click(); in chrome console I get back: [ <a id=​"fbLink" href=​"/​authenticate/​facebook">​ Facebook-konto ​</a>​ ] shouldn't click result in clicking on the link and moving to /​authenticate/​facebook
|
2

Ok I did it like this since the propagation solution did not work for me.

    $(function () {
        $("#bg2InsideFb, #bg2InsideG").click(function () {
            var tmpLink = $(this).find("a").attr("href");
            window.location.href = tmpLink;
        });
    });

Comments

1

I assume you want to trigger #fbLink. In this case you have to use

$('#fbLink').click(function(ev) { 
    ev.stopPropagation();  
});
$('#fbLink').triggerHandler('click');``

2 Comments

No, trigger still causes bubbling. I think you meant .triggerHandler(), but that still doesn't quite answer the OP
My fault, yes I meant triggerHandler(). Thanks for correcting me.
1

You can check for ev.target to see if it's the propagation from the inside element.

$(document).on('click', '.photoplaceholder', function(e){
    if (!$(e.target).closest('INPUT[type="file"]').length) {
        $(this).find('INPUT[type="file"]').click();
    }
});

Comments

0

When you are calling $("#fbLink").click(); it actually calling its parents also. that's why its causing recursion error. Could you please explain actually what are you want to do?

$("#bg2InsideFb").click(function () {
   $("#fbLink").click(function(){
      return false;
   });
});

It may works.

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.