Introduction to Server-side JavaScript [con't]
Coding for Jaxer
To tell the server where to execute the code, you need to add
the runat
attribute to <script>
blocks. This attribute has several possible
values, as follows:
- client: The code will run in the client's browser, just
like regular JavaScript. This is the default value of the
runat
attribute, so you can omit it for script blocks intended for the client.
- server: The code will run on the server. Furthermore, any functions defined within the script block will be cached in association with the page. These functions will not be callable from the client, as only script output is sent to the browser, but they can be called during callback processing by other server-side functions.
- both: The code will run on both the client and the server. However, any functions defined within the script block will still be cached in association with the page so that they are not directly accessible to the client, but they can be called during callback processing by other server-side functions.
Some special Function
object properties can be declared on individual functions
to control how they are managed. These can be used to override the containing
script block runat
setting for the individual function, thus allowing for more
granular control. For instance, server-side functions can be declared
proxied so they are callable from the client side. This is achieved by specifying
a proxy
property on the function object. The possible values for this property
are true
or false
:
The code above groups similar functionality together, while allowing the getClientID()
function to override the default runat="server"
behavior so that it
can be called from the client. This is possible because the runat
attribute
is present. In client scripts with no runat
attribute, the option of programmatically
changing it by setting a different runat
or proxy
value is not available.
Alternatively, the Jaxer namespace provides a useful convenient proxie property
to allow the proxy functions to be declared in a single group within your JavaScript
code. It is an Array
object, so functions can be assigned using any format or
functions that are common to arrays:
Back in the "Debugging JavaScript: Beyond Alerts" article, I stated that we will be looking at some popular JavaScript debuggers and IDEs in up-coming pieces. In the next installment, we will be taking a look at the first of these: the Aptana Studio IDE. We'll be using it to build a simple file-based blog for Jaxer.
Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest, entitled KNIGHTFALL, was a collaboration between himself, the former Ivory Knight vocalist, and legendary guitarist/producer, Jeff Waters of Annihilator fame. Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!
Original: July 9, 2009