The server at your web host (or your own machine if you have a localhost install) has one main job. When a user requests a page (by clicking on a link, or typing in a URL), the server looks for the requested page and sends it to the browser. In the very early days of the Web, that was the whole story, and it’s still the most important part. In those days, the pages were HTML files on the server’s drive and were essentially sent directly to the browser for display.
These days, the process is a little more complicated, but still fairly basic. The server may use the rules in an .htaccess file that alter the page request before looking for the file (or telling a banned user to get lost). In MODX, and some other Content Management Systems, for example, a single index.php file handles all the requests and often gets the page content from a table in the database.
Another common use of JS code, often seen in shopping-cart processing, is leading the user through a series of steps. At the conclusion of each step, the user clicks on a “Next” button, or something similar, and the JS code checks the current page for validity, then hides the current page and reveals (or constructs) the next one. The page is never reloaded during this process. The validation or creation of the next step’s page might involve Ajax, which we’ll get to in a bit.
In the example we saw earlier for shopping-cart processing, at some point, there will be a check to see if the user is already registered on the site. This requires contacting the server (since the browser has no idea whether the user is registered or not). The JS code will send an Ajax request containing enough information to identify the user (e.g., an email address or user ID). The server will check the database, and respond to the Ajax request with information indicating if the user is registered, and if so, some of the user’s information (e.g. shipping address) that can be used to populate the next page.
It doesn’t have to be, but the response to an Ajax request is almost always in the form of a JSON string. JSON is simply a method of encoding information in the form of a string of plain text. The user list information described above, might come back looking like this:
Here’s the same JSON string formatted to make it a little easier to read:
JSON strings can contain simple information like the example above, or extremely complex nested arrays of data containing dates, strings, numbers.
Applying this to MODX
pagetitle = "[[*pagetitle]]";
MODX would replace the tag with the actual pagetitle of the page being accessed before the page is sent to the browser and that pagetitle would be the value of the
JS in the MODX Manager