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: http://www.codeproject.com/KB/ajax/SimpleAJAX.aspx
  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=document.forms[0];
frm.method="GET"; // POST

frm.action="ServerSidePage.jsp";//Some servlet/Some CGI Script/URL
frm.submit();
}

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='http://www.google.com'" 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="http://www.sabahmyrsh.blogspost.com" select e d="selected">select an item from List:< o p t i o n value="http://www.alislam.org">AlIslam

No comments: