|
|
Enabling remote scripting on the server side
Our client_test.html page includes JavaScript
that is executed when a button is clicked on. This JavaScript contains a
call to RSGetASPObject, which takes rev.asp as its first parameter.
Create a new file called rev.asp and add the code shown below to rev.asp
as we go. In order for rev.asp to participate in remote scripting, it must
include the server version of rs.htm, which is rs.asp. Rs.asp includes a
method called RSDispatch, which must be called immediately after rs.asp is
included to initialise remote scripting:
<!--#INCLUDE
FILE="_ScriptLibrary/RS.ASP"-->
<% RSDispatch %>
Next we create a block of server side JavaScript code, like this:
<script language="JavaScript"
runat="server">
var public_description = new MainMethod();
function MainMethod()
{
this.revStr = Function('strString','return reverse(strString)');
}
</script>
The code above creates a new object called public_description, which is an
instance of a function called MainMethod. MainMethod is a constructor that
contains a list of functions and how they should be mapped so that remote
scripting can be used to call them. Note that the MainMethod function can
be called anything you like.
Before we continue, add the following block of VBScript at the end of
rev.asp:
<script language="VBScript"
runat="server">
function reverse(strString)
reverse = strReverse(strString)
end function
</script>
If you now take a look at the code inside of MainMethod, then you will see
that it creates a new method called revStr, which maps to our VBScript
reverse function:
this.revStr = Function('strString','return
reverse(strString)');
Whenever a client calls the revStr method, remote scripting will
automatically call our VBScript function reverse. The Function() method is
used to define the revStr method. Its first argument is the parameter(s)
that the function we're mapping to accepts. Our VBScript reverse function
accepts one parameter, and we specify it using 'strString'. Next we have
the function signature, which specifies the return keyword as well as the
function name we're mapping and its parameters.
Jumping back to our client_test.html, we have this code:
function reverseString()
{
var strTest = prompt("Enter String To Reverse:");
var objRS = RSGetASPObject("rev.asp");
var objResult = objRS.revStr(strTest);
alert(objResult.return_value);
}
As you can see, it uses RSGetASPObject to create a new object containing
all of the functions that we've defined in rev.asp. Because we've mapped
our VBScript function reverse as revStr, we call objRS.revStr with one
parameter, which is the string that it should reverse and return.
A call to any method of an object created from a call to RSGetASPObject
returns an object itself. This object exposes several functions and
variables, most notably return_value, which is the value returned from the
call to the specific function (which in our case is revStr). In our
client_test.html page, we use JavaScript's alert function to display the
returned value in a message box.
Here's the entire code for rev.asp:
<!--#INCLUDE
FILE="_ScriptLibrary/RS.ASP"-->
<% RSDispatch %>
<script language="JavaScript" runat="server">
var public_description = new MainMethod();
function MainMethod()
{
this.revStr = Function('strString','return reverse(strString)');
}
</script>
<script language="VBScript" runat="server">
function reverse(strString)
reverse = strReverse(strString)
end function
</script>
When I ran client_test.html in my web browser and entered "the sky is
blue" into the popup window, it looked like this:
Just to prove that remote scripting also works in Netscape, here's a
screenshot from Netscape 4.74:
Hopefully you've now got our first sample up and running. Let's build on
what we've learnt so far and add some database functionality to the mix.
-Tutorial introduction
-What is remote scripting?
-Testing remote scripting
-Enabling remote scripting on the server
side
-Another remote scripting example
Another
remote scripting example
|