127

I am getting the error "Uncaught RangeError: Maximum call stack size exceeded" on chrome. here is my jQuery function

$('td').click(function () {
        if ($(this).context.id != null && $(this).context.id != '') {
            foo($('#docId').val(), $(this).attr('id'));
        }
        return false;
    });

Note that there are tens of thousands of cells in the page. However, I generally associate stack overflows with recursion and in this case as far as I can see there is none.

Does creating a lambda like this automatically generate a load of stuff on the stack? is there any way round it?

At the moment the only workaround I have is to generate the onclick events explicitly on each cell when rendering the HTML, which makes the HTML much larger.

Andy
  • 10,412
  • 13
  • 70
  • 95
  • 2
    Are you sure the foo function doesn't recurse? Does the error still happen if you remove that function call? – sth Oct 05 '11 at 08:58
  • 1
    Does it work as expected in other browsers? Does this error occures when you comment the `foo($('#docId').val(), $(this).attr('id'));` line? -- Extra performance tip: cache the result of selectors - for example keep the result of `$(this)` in a variable and than use it across your handler as needed. – WTK Oct 05 '11 at 09:00
  • I have a similar issue but need mouseenter events. When using body or table I dont get enough events. – ericslaw Oct 29 '15 at 17:32

10 Answers10

140

As "there are tens of thousands of cells in the page" binding the click-event to every single cell will cause a terrible performance problem. There's a better way to do this, that is binding a click event to the body & then finding out if the cell element was the target of the click. Like this:

$('body').click(function(e){
       var Elem = e.target;
       if (Elem.nodeName=='td'){
           //.... your business goes here....
           // remember to replace $(this) with $(Elem)
       }
})

This method will not only do your task with native "td" tag but also with later appended "td". I think you'll be interested in this article about event binding & delegate


Or you can simply use the ".on()" method of jQuery with the same effect:

$('body').on('click', 'td', function(){
        ...
});
Andrew Ng
  • 340
  • 2
  • 12
vantrung -cuncon
  • 10,207
  • 5
  • 47
  • 62
  • thanks, we're struggling for performance on this anyway, so this is a great idea :-) – Andy Oct 05 '11 at 13:33
  • 62
    Nooo, don't use .live()!!! http://bitovi.com/blog/2011/04/why-you-should-never-use-jquery-live.html Use a .delegate() (or .on() if your jQuery is new enough), and delegate from the table level rather than the entire document. That will improve your performance much more than just using .live(), which will essentially just delegate from the entire document down. – brandwaffle Apr 30 '12 at 21:30
  • 21
    And .live has been removed from jQuery 1.9 – cpuguy83 Feb 22 '13 at 16:03
39

You can also get this error when you have an infinite loop. Make sure that you don't have any unending, recursive self references.

John Durden
  • 391
  • 3
  • 3
8

Mine was more of a mistake, what happened was loop click(i guess) basically by clicking on the login the parent was also clicked which ended up causing Maximum call stack size exceeded.

$('.clickhere').click(function(){
   $('.login').click();
});

<li class="clickhere">
  <a href="#" class="login">login</a>
</li>
Raptor
  • 53,206
  • 45
  • 230
  • 366
Jsonras
  • 1,120
  • 13
  • 10
3

This problem happened with me when I used jQUery Fancybox inside a website with many others jQuery plugins. When I used the LightBox (site here) instead of Fancybox, the problem is gone.

Silvio Delgado
  • 6,798
  • 3
  • 18
  • 22
1

U can use

  $(document).on('click','p.class',function(e){
   e.preventDefault();
      //Code 
   });
demenvil
  • 1,089
  • 1
  • 12
  • 25
1

I recently just ran into this issue as well. I had a very large table in the dialog div. It was >15,000 rows. When the .empty() was called on the dialog div, I was getting the error above.

I found a round-about solution where before I call cleaning the dialog box, I would remove every other row from the very large table, then call the .empty(). It seemed to have worked though. It seems that my old version of JQuery can't handle such large elements.

dev4life
  • 880
  • 2
  • 8
  • 18
0

I was getting this error because of my mistake that I forgot to declare one of the variable which was passed in Ajax data.Only mode was declaredat first.

data: {
     tmp_id: tmp_id,
     mode: mode
}

Declared the tmp_id variable also and it worked fine.

let tmp_id=$("#tmp_id").val();
let mode=$("#mode").val();
$.ajax({
    url: 'my-url',
    method: 'post',
    data: {
     tmp_id: tmp_id,
     mode: mode
    }
    dataType: 'json',
    success: function(response) {
       console.log(response);
    }
    });
 }
RoshJ
  • 461
  • 1
  • 6
  • 24
0
   In My case I was html element insted of value
                          |
                          |
   var uname=$("#uname");<-
    
   let data={
     uid:uid,
     .....
   };        
    
   $.post("url",data,(responseText)=>{ 
       ..
   }).fail((xhr)=>{
       ..
   });
    
  then I updated 
                              |
                              |
  var uname=$("#uname").val()<- 
  • 1
    Hey, this question is already answered and is quite old. Have a look at https://stackoverflow.com/editing-help to properly format your answers in the future – Moudi Dec 18 '22 at 11:47
0

Make sure you are not accessing elements without obtaining their values. I've been able to easily reporduce this issue due to the omission of the val() function.

For example:

$.ajax({
    url : '/url-test',
    type : 'GET',
    data : {
        'country':$("#test_input_field")
    }

Should be

$.ajax({
    url : '/url-test',
    type : 'GET',
    data : {
        'country':$("#country__c").val()
    }
AdamJones
  • 652
  • 1
  • 15
  • 38
0

For me, I was trying to reference $('#inputfield') when I should have instead referencing $('#inputfield')**.val();

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
user3163139
  • 27
  • 1
  • 5
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34797250) – XMehdi01 Aug 10 '23 at 22:25