Home arrow ASP arrow Page 4 - Remote Scripting With JavaScript and ASP
ASP

Remote Scripting With JavaScript and ASP


Remote scripting allows us to use a combination of scripting languages to call server side ASP code from the client using JavaScript. In this article, Annette shows us how...

Author Info:
By: Annette Tennison
Rating: 5 stars5 stars5 stars5 stars5 stars / 78
December 14, 2002
TABLE OF CONTENTS:
  1. · Remote Scripting With JavaScript and ASP
  2. · What is remote scripting?
  3. · Testing remote scripting
  4. · Enabling remote scripting on the server side
  5. · Another remote scripting example
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Remote Scripting With JavaScript and ASP - Enabling remote scripting on the server side
(Page 4 of 6 )

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:

Running client_test.html in IE

Just to prove that remote scripting also works in Netscape, here's a screenshot from Netscape 4.74:

Running client_test.html in Netscape

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.
blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials