Peter Penzov
Peter Penzov

Reputation: 1668

Too much recursion in JavaScript

I have this JavaScript which opens new page:

$(document).ready(function () {
    //$('a[id$="lnkHidden"]').trigger("click");    // Not sure if this is actually necessary

    $('table[id$="dataTable"]').find("tbody").on("click", "tr", function () {
        $(this).find('a[id$="lnkHidden"]').trigger("click");
    });
});

This is the button which is called by the JS script:

<h:commandLink id="lnkHidden" action="#{bean.pageRedirect}" style="text-decoration:none; color:white; display:none">

</h:commandLink>

After I click on a table row I get this error message:

too much recursion [Break On This Error] ...,c=l.length;c--;)(f=l[c])&&(v[d[c]]=!(y[d[c]]=f));if(i){if(o||e){if(o){for(l=‌​[],...

Can you help me to fix this?

Upvotes: 1

Views: 408

Answers (2)

bfavaretto
bfavaretto

Reputation: 71908

Instead of triggering synthetic click events, you could just change the current URL directly:

$(document).ready(function () {
    $('table[id$="dataTable"]').find("tbody").on("click", "tr", function () {
        var links = $(this).find('a[id$="lnkHidden"]');
        if(links.length && links[0].href) {
            window.location.href = links[0].href;
        }
    });
});

Upvotes: 1

atondelier
atondelier

Reputation: 2434

You can cut the infinite loop with those changes from your original code

  • add a second argument to trigger. The call becomes .trigger("click", [ true ])
  • name arguments in the event handler : function(event, simulated)
  • use the simulated argument which is set to true from the trigger : simulated || $(this).find('a[id$="lnkHidden"]').trigger("click", [ true ]);

However that event triggering and that kind of selectors are not recommended.

Upvotes: 1

Related Questions