0

I would like to gather all useful undocumented methods and properties of jQuery (at least for the current version 1.9.0).

Some of them look quite promising either for solving particular problems, or for accelerating the JavaScript code evaluation.

Important: Since the listed features here are not documented, it should be used with caution, as new versions of jQuery might not support these features in future releases.

A1rPun
  • 14,111
  • 6
  • 51
  • 79
VisioN
  • 132,029
  • 27
  • 254
  • 262
  • this has to become a CW or it will get closed. Which I saw you do in your response :) – Woot4Moo Jan 15 '13 at 19:34
  • 4
    Encouraging people to rely on undocumented features? What could go wrong! – Malvolio Jan 15 '13 at 19:34
  • It's undocumented because it's totally new... – marcos.borunda Jan 15 '13 at 19:36
  • 1
    Also that link is going to be quite useless when they roll a new version. You should explicitly call out the version. – Woot4Moo Jan 15 '13 at 19:36
  • 4
    jQuery's undocumented features are that way because they're considered private to the library. Relying on what amounts to private functions/variables is a good way to write bad code, as an update might suddenly ruin your plugin. – zzzzBov Jan 15 '13 at 19:40
  • @zzzzBov The interesting thing is that most of features I found are either not changed from the first versions, or/and are used externally by such libraries as jQueryUI. However, to fix the misunderstanding I added the current version in the question. – VisioN Jan 15 '13 at 19:43
  • I would call that coincidence vision. – Woot4Moo Jan 15 '13 at 19:45
  • 2
    @VisioN, it's good you added that disclaimer, but it points out the inconsistency of this question: is a list of methods you should use with caution (i.e. not at all if you can) really useful? – Frédéric Hamidi Jan 15 '13 at 19:53
  • 1
    @FrédéricHamidi I don't think so. Two years ago people were suggesting to use `$.browser` or `:radio` selector, while now it is deprecated and totally removed from the current release. I don't think that we need to think in that way now, while we *can* use what is supported now. Moreover, what was removed we can always check in the [track](https://github.com/jquery/jquery-migrate/blob/master/warnings.md) (including internal features). – VisioN Jan 15 '13 at 20:01
  • @zzzzBov, You may disagree it's a good idea using undocumented functions which I agree, it's not a good idea most of the time and for most people, but calling it _"not constructive"_ like in your close vote?! **Voted for reopening!** I have a very useful function which isn't documented but jQuery do use in their demos, but your close vote prevents me from sharing it. Closing 28K user's question as not constructive is silly, I believe he knows the rules. – gdoron is supporting Monica Jan 19 '13 at 23:46
  • 1
    @gdoron, [don't assume that just because someone has 28K rep on SO that they inherently ask perfect questions](http://yourlogicalfallacyis.com/appeal-to-authority). My close vote was because I felt that the question wasn't a good fit for the [SO] Q&A format (there's no one right answer), and because it was likely going to solicit debate and or extended discussion. *This right here is debate and extended discussion*. – zzzzBov Jan 19 '13 at 23:58

1 Answers1

5

$.active

Counter for holding the number of active queries

This property is useful when there is a need to check the number of running Ajax requests. For example, the following code can be used to prevent double Ajax request:

$("button").on("click", function() {
    if ($.active.length === 0) {
        $.ajax({ ... });
    }
});

$.camelCase(string)

Convert dashed to camelCase

While jQuery uses this method in css and data modules, you can use it to transform strings in format xxx-yyy-zzz to xxxYyyZzz:

var str = $.camelCase("xxx-yyy-zzz");  // "xxxYyyZzz

$.easing

This property contains all supported easing effects with functions for calculating these effects. By default jQuery supports linear and swing effects only, but if you include jQueryUI $.easing property will be extended with other effects.

For instance, we can easily check if bounce effect is supported with the following code:

var bounceSupported = "easeInOutBounce" in $.easing;

$.isReady

Is the DOM ready to be used? Set to true once it occurs.

This property can be used as a flag to check if DOM was fully loaded, if for some reason you don't use $(document).ready() event. Easy example so far:

if (!$.isReady) {
    $.error("DOM was not fully loaded yet");
}

$.readyWait

A counter to track how many items to wait for before the ready event fires.

Yet I found no other use except the same as for $.isReady.


$.text(elem)

Utility function for retrieving the text value of an array of DOM nodes.

Fast method for picking up text contents from DOM tree. It is useful when you need to speed up your code, that works with DOM elements:

$("div").on("click", function() {
    console.log( $.text(this) );
});

$.timers

This property holds timers of jQuery elements animation. It can be used for fast check if there is any animation process running at a time, instead of $(":animated").length:

$("span").on("click", function() {
    if ($.timers.length === 0) {
        $(".flows, #spoiler, #par > p").fadeIn("slow");
    }
});

One good usage example can be taken from this question.


$().push

This method behaves in the same way as Array.push(), adding new DOM element to jQuery object. Does the same as $a = $a.add($b):

var $a = $("#element1"),
    $b = $("#element2");

$a.push($b[0]);

Usage example: jQuery concatenation of selectors without creating a new instance?

Community
  • 1
  • 1
VisioN
  • 132,029
  • 27
  • 254
  • 262