JQuery $ (this) .position (). not working in Chrome.

I was wondering if there would be any reason that this piece of code would not work ...

top = $(this).find('ul').position().top; 

It works in IE, Firefox and Safari, however, when I warn about the exit in Chrome, it talks about the DOM window object ... I need an integer. Why should it notify the DOM window object?

+4
source share
2 answers

Are you sure you are really warning that top ? Recall that top is an existing global variable in a browser hosted in JavaScript (this is window.top , a top-level window).

Refresh . Interestingly, Chrome won't let you implicitly overwrite top (which is probably good): Demo . Just declare your variable (always a good idea anyway) within any function the code is in (this code is in the function, right?), Which will shadow it, and it will work this way: Demo . For instance:.

 var top = $(this).find('ul').position().top; 

It is important to declare your variables so as not to fall prey to the Horror of implicit globals . And, as you found in this case, avoiding global bindings is always better, since Chrome will not even allow you to use top as global if you declare it (protect window.top again).

+12
source

Make sure you assign the value to a variable with local scope. Use the var keyword.

 var top = $(this).find('ul').position().top; 
+2
source

All Articles