PX - jQuery Comparison
  • Differences

    pxObject can be used with a syntax very like the jQuery JavaScript library, but there are some differences.
    • The Document

      While jQuery uses the loaded HTML document, you have to specify the xml you want to work with in a pxObject.
    • Object Accessor

      Obviously, in PHP you must use the -> notation to access methods and member variables (rather than the . notation, used in JavaScript).
    • Node Selection

      The PX library uses xpath to select nodes on which to operate, as opposed to jQuery's simplified, shorthand method.
    • Available Functions

      The pxObject implements only DOM manipulation functions. There's no place in server-side processing for client-side functions.

Comparison to jQuery

The PX library was written to provide DOM manipulation functionality using a set of methods (and a syntax) similar to that of the jQuery JavaScript library. It is definitely not meant to replace jQuery - only to provide an equally convenient syntax for manipulating DOM XML on the server.

This document outlines the minor, unavoidable differences between the PX syntax and that of jQuery.

Explicit Document Loading

JQuery is designed to work with the document currently loaded in the client web browser, but the pxObject can't do that. PX actually generates the document that jQuery will work with, so we need to provide our px objects with XML to work with.

Provide PX the XML it is to work with by passing a string of xml, a file path pointing to an XML document, a PHP DOMDocument, or a single PHP DOMNode. After you specify the XML, you can use PX much like jQuery for DOM manipulation.

$aPxObject = new px("myDocument.html");

Object Access Notation

Obviously, in a PHP environment, you have to use the "->" arrow notation, rather than the JavaScript "." dot notation, to access object methods and variables.

// jQuery
$myJqObject.append( "<b>That's a bold tag!</b>" );
// PX
$myPxObject->append( "<b>That's a bold tag!</b>" );

PX Uses X-Path

JQuery makes use of a simplified shorthand method of selecting DOM nodes to operate on. This method is not available to pxObjects. The PX library uses standard X-Path selection.

// jQuery
$myBoldTags = $("b");
// PX
$myBoldTags = $doc->xpath("//b");

Callback Function Arguments

JQuery sets and calls callback procedures as methods of the object being operated on. PX is limited to calling user-defined functions as functions, so using $this is not possible. Instead, all built-in pxObject methods pass themselves ($this) as the first parameter to user-defined callback functions, and the zero-based index of which "matched" element is currently being processed.

Another minor difference is that in jQuery, DOM manipulation methods such as html() empty the specified node before calling the user callback function. They pass the existing HTML to the function so that the it can be returned if the function decides not to use it. In the PX counterparts to such methods, the function return results are applied only if they are actually returned. If your function decides no change is necessary, it should simply refrain from returning any value at all.

The way jQuery handles it is somewhat more acceptable for client-side processing, but a busy server needs as much speed as it can get.

Other Functionality

JQuery provides a great set of features - some are applicable to server-side processing, others strictly useful on the client side. Of course, the pxObject class implements only those functions that are useful on the server side. Namely, the DOM Manipulation functions.

For a complete list of documented functions (pxObject methods), see the API Method List page.