3

jQuery has a native .position() function right out of the box which gets positioning values of an element relative to its location on the page.

jQuery UI extends that same function to allow modification of positioning of elements.

How do I check if the jQuery UI version of .position() is loaded in a page?

Sadly, it isn't just a matter of checking whether jQuery UI is loaded on a page or not, since the .position() plugin extension is not part of the jQuery UI Core libraries, which means someone somewhere will have a jQuery UI framework loaded without the .position() plugin, and the linked jQuery UI check will fail.

Community
  • 1
  • 1
Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66

2 Answers2

2

The major difference is between the number of arguments in the function definition. The standard jQuery library has 0; jQuery UI has 1. If you can tell how many arguments are in the function definition, you can tell which library's function is present.

Fortunately, this is easy to do. A function's length property tells you how many arguments were in the function definition. So if $.fn.position.length is 0, the function is that of the standard jQuery library; if it is 1 it is the jQuery UI library.

So:

if ($.fn.position.length === 1) {
    // jQuery UI position method loaded
}
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • Thanks for sharing, and TIL about `(function).length` in Javascript. I found another way, and I kind of prefer that over this (a bit because it's more readable for me), but I'll wait just in case somebody throws a fit about my solution about compatibility or whatnot. :D Much appreciated! +1 – Richard Neil Ilagan Sep 11 '12 at 20:54
  • @RichardNeilIlagan No, your way is much better! – lonesomeday Sep 11 '12 at 21:00
1

Woops, wait; I think I found it.

Pages with jQuery UI loaded will indeed have jQuery.ui initialized (which will do fine for jQuery UI checks), but those with the .position() plugin baked in as well will have jQuery.ui.position initialized as well.

Figures that I find the answer 5 minutes after asking on SO. :-/

Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66