Monday, September 1, 2008

JavaScript: Ways to Hit Server Side using JavaScript

There are three ways to hit the server side application using JavaScript that may be some servlet/JSP/CGI Script. Following are the ways:

  1. Use AJAX to hit the Server Side. You can get the details on this from following post:
  2. Second way is to submit the form data using the JavaScript. Following sample JavaScript function will perform the required task. Make sure you place this function in your HEAD tag of HTML in a script tag.

function goForward() {
// alert ("Go Forward ..... ");
frm.method="GET"; // POST

frm.action="ServerSidePage.jsp";//Some servlet/Some CGI Script/URL

3. You can use the "document.location.href", and you can specify the server side path of any servlet/JSP/ CGI Script/ Other URL.

Example No:1
The following code segment will display an button on your page and when u will click on it, it will open the Google page.

"< i n p u t name="myButt" value="Call Server Side" onclick="document.location.href=''" type="button" >

This example will look like this:

Example No:2

In this example, we select the an item from the drop down list and it will go to the page that you have specified in the value of selected option.

Place the following method in your HEAD section of HTML:

function callURL()
document.location.href= document.formName.SelectListID.options[document.formName.SelectListID.selectedIndex].value

Andplace the following code in body section of your HTML:

< f o r m name="formName">
< s e l e c t name="SelectListID" onchange="callURL();"> < o p t i o n value="" select e d="selected">select an item from List:< o p t i o n value="">AlIslam