Okay this week i'm going to be covering one of the least used and arguably most useful asp trick that shipped with visual interdev. RemoteScripting is one of those annoying things that get installed by visual interdev in it's _ScriptLibrary. personally i hate code that gets written for me without knowing what the code actually does so i always delete those underscore folders that interdev creates.today i'm going to tell you not to delete this folder just yet, saving the remote scripting content and this sample will show why this will provide you a very nifty trick. Historically speaking remotescripting has been around for two years so it's not a new technology and in the future the introduction of SOAP (Simple Object Access Protocol) will phase this technology out. But for now it can be very very handy. in my opinion the main reason why remotescripting has never been widely used by ASP programmer is because of the fact that in it's early implementation only server side JScript could be used and heaps of people implement ASP in VBScript so it never experienced full exposure (among other reasons). okay with this in place i'll continue... Ever wanted to validate a primary key without actually posting a form? Ever wished that you can call a function on the server from your client script without submitting? if you ever seeked to achieve the above and always came to the conclusion that it's just not possible in ASP then RemoteScripting is your savor, yes RemoteScripting is exactly what it's name implies. Remotescripting is a method to communicate with remote script over HTTP. in this article i'm going to be showing an example of calling a function in an ASP page from a client-side javascript on a HTML page. The sample will show an example of a simple function call with just a boolean return, it will also show a more advance method of calling a function on the server and passing it parameters. Now is a good time to state this, RemoteScripting is browser independent (yes Netscape) and with VBScript 5.X it's server class can be implemented with VBScript. The Sample #1 On the server in a page called remote.asp i have the following function, this file will be showed later on. function SimpleFunction(){ return true; } |
Now to demonstrate the remote in remotescripting, clicking the below button will trigger this function from this page without posting The code
objTest = RSExecute("remote.asp","SimpleFunction")
alert(objTest.return_value); |
above is the client side javascript code that was written to call the remote function on the server, first of all Remotescripting must be enabled this is done by including the following code into the head of the calling page.
<scriptlanguage="Javascript"src= "./_ScriptLibrary/rs.htm"> </script>
<scriptlanguage="Javascript"> RSEnableRemoteScripting("./_ScriptLibrary");</script> |
once enabled using the RSExecute function, any remote scripting class in a asp page can have it's methods called and the result returned. The remote class can be on your server or another server providing you this service.
#2 Type text into the textbox belox and click the "Call Remote Function" The Code
objTest = RSExecute("remote.asp","ParamFunction",document.frmRemote.txtParam.value)
alert(objTest.return_value); |
The second function is slightly more advance it takes parameters enter in the text box and passes them to the ParamFunction on the server. Now the function on the server can be anything you want them to be they could validate a value etc... so you have seen what is on the client, here's the content on the server.
The Server Class <%@ LANGUAGE=JScript%>
<%
//the remote class
function clsRemoting()
{
this.SimpleFunction = _SimpleFunction;
this.ParamFunction = _ParamFunction;
}
//the is the function that getting called
function _SimpleFunction(){
return true;
}
function _ParamFunction(strvalue){
return ("Hi from " + Request.ServerVariables("SERVER_NAME") +
" this is the paramter passed " + strvalue);
}
var public_description = new clsRemoting();
// Call RSDispatch to use the public_description object
//and make its methods available for Remote Scripting Calls
RSDispatch(); %>
|
|