tags:

views:

37

answers:

1

Hi, I think this may be a "false positive", but I could be wrong. I have the following script and it's crashing on one line with a "too much recursion" error:

var Win, Doc;
var Content, Blackout;

$(function () {
    Win = $(window);
    Doc = $(document);

    Content = $("#Content");
    Blackout = $("#Blackout");

    Content.bind("resize", function () {
        Content.css({
            minHeight: ((Win.height() - Content.position().top) - 20)
        })

        Blackout.trigger("resize"); // <- this is where the error appears
                                    //    to be triggering
    }).trigger("resize");

    Blackout.css({
        opacity: .2
    }).bind("resize", function () {
        Blackout.css({
            height: Content.innerHeight(),
            width: Content.innerWidth()
        });
    }).bind("click", function () {
        $("div.Wizard:visible").trigger("hide");
    }).trigger("resize");

    Win.bind("resize", function () {
        Content.trigger("resize");
    });
});

From reading the other topics on this, I can only assume that once Blackout.trigger("resize") is called, it will query Content via the innerHeight() function which Firefox/Firebug could be interpreting as recursion and thus crashing it. I could be wrong, but that's what I've come up with.

Either way, I would appreciate some help on this.

+4  A: 

If #Blackout is inside #Content (I'm guessing by the named here) then then event is bubbling up and causing the resize handler you're in to fire again.

Instead of .trigger() invoke it in a way that doesn't bubble using .triggerHandler(), like this:

Blackout.triggerHandler("resize"); 

This way that resize event won't trigger the resize handler on #Content...which causes your loop.

Nick Craver
You're correct, Blackout is inside Content. And thank you for clarifying, I now understand why is recurring. It's essentially because both are named `resize` the Blackout event bubbles up and hits the Content event which then triggers Blackout and on and on...
Alex
On a side note, is it better to use `triggerHandler` over `trigger`?
Alex
@Alex - If you don't want to event to bubble and you *only* want the handler on the element to execute, yes. In many other cases you just want `.trigger()` so other code can attach events, potentially at a higher level...in cases you *want* bubbling.
Nick Craver
I see. Thank you for your help!
Alex