Remote JavaScript

The primary reason is that the round trip time required to submit a form and then download the response is often inconvenient. The user experience is much improved if, instead of clicking a Submit button and watching the screen go blank and then be replaced by the response of a server-side program, a user can click a button and have the page be updated without a visible form submission. To the user, the page would behave more like an application than a Web page. 

There are other advantages as well. If communication with a server can be done behind the scenes instead of using form submissions or clicking on links, the developer can carry out more complicated tasks requiring multiple server requests at once. The ability to use remote JavaScript also means that tasks that previously required an ActiveX object or Java applet can be implemented with script. This is a tremendous timesaver for the developer and also reduces the complexity of debugging significantly. 

The abstraction that remote JavaScript brings to life is the remote procedure call. A remote procedure call (RPC) is a function that executes on a remote machine, in this case a Web server. The client, in this case our browser using JavaScript, passes arguments to the “function” it wishes to call via an HTTP request; the server executes the specified function, often implemented as a CGI program or server-side script in PHP or a similar language, and returns the results as the body of the HTTP response. It’s important to remember that while JavaScript is used to make the function call and often to handle the return value, the function itself executes on the server, and therefore can be implemented as a CGI, PHP script, Java servlet, or using any other technology a Web server might have available.

By far the most common vehicle used for one-way communication is images. You don’t even have to use <<img>>s embedded in the page; in fact, it’s preferable not to. Since you don’t intend to actually download or display an image, you can create an Image object, use it, and then throw it away. Consider the following document fragment:
<<script type="text/javascript">>

var commandURL = "http://demos.javascriptref.com/setrating.php?";

function sendURL(url) 

{

  var img = new Image();

  img.src = url;

}

function sendRating(productid, rating, user) 

{

  var params = "productid=" + productid;

  params += "&rating=" + rating;

  params += "&user=" + user;

  sendURL(commandURL + params);

  return false;

}

<</script>>

<<!-- ... -->>

Rate this item:

<<form action="#" method="get">>

  <<input type="button" value="Horrible!" onclick="return sendRating(2158, 1,

 'Bob');" />>

  <<input type="button" value="OK" onclick="return sendRating(2158, 2,

 'Bob');" />>

  <<input type="button" value="Great!" onclick="return sendRating(2158, 3,

 'Bob');" />>

<</form>>

Note 
Don’t think that because we are using form buttons here that this is a traditional style communication. In fact, we could have used just about any object we could click by attaching an onclick, including links (<<a>>) or even structural elements like <<div>> or <<p>>, but we used form buttons since the user would feel they were clickable!

Related Posts:
  • JavaScript Popups <head><script type="text/javascript">function testPopup() {window.open( "http://www.google.com/" )}</script></head><body><form><input type="button" onClick="testPopup()" value="Click"&g… Read More
  • JavaScript Form Validation  JavaScript Form Validation. JavaScript can be used to validate data in HTML forms <script type="text/javascript"> function validate_all()     {     var frmReg=document.manageadmin… Read More
  • Function Call Spacing Almost universally, the recommended style for function calls is to have no space between the function name and the opening parenthesis, which is done to differentiate it from a block statement. For example: // Good doSom… Read More
  • Embedding Content for Plug-Ins Although never officially a part of any HTML specification, the <<embed>> tag is most often used to include embedded objects for Netscape and Internet Explorer. A Macromedia Flash file might be embedded as fo… Read More
  • Javascript Image Objects Properties of Image Objects Property Description align Indicates the alignment of the image, usually “left” or “right.” alt The alternative text rendering for the image as set by the alt attribu… Read More
  • JavaScript Refresh Page you refresh the page using document.location.reload(). You can add the true keyword to force the reloaded page to come from the server (instead of cache). Alternatively, you can use the false keyword to reload the page from … Read More
  • JavaScript and XML To demonstrate JavaScript, XML, and the DOM in action, let’s use Internet Explorer 5.5 or better to load an XML document containing our employee directory and see if we can manipulate it. First, to load in the document w… Read More
  • Javascript Making a simple MouseOver <a href="http://www.cit.cornell.edu"onMouseOver="document.logo.src='family.gif ' ; "onMouseOut ="document.logo.src='harry.gif ' ; " ><img name="logo" src="harry.gif " border=0></a> The onMouseOver and onM… Read More
  • Javascript Math object The Math object holds a set of constants and methods enabling more complex mathematical operations than the basic arithmetic operators var root = Math.sqrt(10); Constants Provided by the Math Object Property … Read More
  • Remote JavaScript The primary reason is that the round trip time required to submit a form and then download the response is often inconvenient. The user experience is much improved if, instead of clicking a Submit button and watching the … Read More
  • JavaScript Dynamic Links and Menus Many sites use JavaScript to create links to other website pages. Here is some examplecode with different link types that you may want to avoid: <HTML><head><title>Link Examples ~ Things to stay away from&l… Read More
  • Javascript Objects as Arguments You may want to write functions that take many arguments, some ofwhich are optional.function drawElement( color, border, width, height,left, top, zIndex) {// Make and display an element with these variables}drawElement("red"… Read More
  • Javascript Recursion Recursion is when a function calls itself. This is often useful in mathematics,such as fi nding the nth number in the Fibonacci series (1, 2, 3, 5,8, 13, 21…).function fi bonacci(n) {if ( n < 2 ) {return 1;} else {return … Read More
  • Javascript search search(regexp)The search() method is the same as indexOf() except that it takes aregular expression pattern instead of a substring. It also returns -1 if thepattern isn’t found."hello world".search(/[aeiou]/); // Find the fi… Read More
  • Javascript The + Operator The binary + operator adds numeric operands or concatenates string operands: 1 + 2 // => 3"hello" + " " + "there" // => "hello there""1" + "2" // => "12" When the values of both operands are numbers, or are both str… Read More