Remember Me
forgot your password?

Alternative to Ajax

By now, nearly everyone who works in web development has heard of the term Ajax, which is simply a term to describe client-server communication achieved without reloading the current page. Most articles on Ajax have focused on using XMLHttp as the means to achieving such communication, but Ajax techniques aren't limited to just XMLHttpRequest. There are several other methods to achieve what AJAX can give to the end-user.
Dynamic Script Loading
The first alternate Ajax technique is dynamic script loading. The concept is simple: create a new element and assign a JavaScript file to its src attribute to load JavaScript that isn't initially written into the page. The beginnings of this technique could be seen way back when Internet Explorer 4.0 and Netscape Navigator 4.0 ruled the web browser market. At that time, developers learned that they could use the document.write() method to write out a tag. The caveat was that this had to be done before the page was completely loaded. With the advent of the DOM, the concept could be taken to a completely new level.
The Technique
The basic technique behind dynamic script loading is easy, all you need to do is create a element using the DOM createElement() method and add it to the page:
var oScript = document.createElement("script");oScript.src = "/path/to/my.js";document.body.appendChild(oScript);
Downloading doesn't begin until the new element is actually added to the page, so it's important not to forget this step. (This is the opposite of dynamically creating an element, which automatically begins downloading once the src attribute is assigned.)
Once the download is complete, the browser interprets the JavaScript code contained within. Now the problem becomes a timing issue: how do you know when the code has finished being loaded and interpreted? Unlike the element, the element doesn't have an onload event handler, so you can't rely on the browser to tell you when the script is complete. Instead, you'll need to have a callback function that is the executed at the very end of the source file.
Example 1
Here's a simple example to illustrate dynamic script loading. The page in this example contains a single button which, when clicked, loads a string ("Hello world!") from an external JavaScript file. This string is passed to a callback function (named callback()), which displays it in an alert. The HTML for this page is as follows:

Example 2
// -1) {
sUrl += "&";
} else {
sUrl += "?";
}
sUrl += encodeURIComponent(sName) + "=" + encodeURIComponent(oParams[sName]);
}
var oScript = document.createElement("script");
oScript.src = sUrl;
document.body.appendChild(oScript);
}
function messageFromServer(sText) {
alert("Loaded from file: " + sText);
}
function getInfo() {
var oParams = {
"name": document.getElementById("txtInput").value, "callback": "messageFromServer"
};
makeRequest("example2js.php", oParams);
}
//]]>


The JavaScript file example1.js contains a single line:
callback("Hello world!");
When the button is clicked, the makeRequest() function is called, initiating the dynamic script loading. Since the newly loaded script is in context of the page, it can access and call the callback() function, which can do use the returned value as it pleases. This example works in any DOM-compliant browsers (Internet Explorer 5.0+, Safari, Firefox, and Opera 7.0.

More Complex Communication
Sometimes you'll want to load a static JavaScript file from the server, as in the previous example, but sometimes you'll want to return data based on some sort of information. This introduces a level of complexity to dynamic script loading beyond the previous example.
First, you need a way to pass data to the server. This can be accomplished by attaching query string arguments to the JavaScript file URL. Of course, JavaScript files can't access query string information about themselves, so you'll need to use some sort of server-side logic to handle the request and output the correct JavaScript. Here's a function to help with the process:
function makeRequest(sUrl, oParams)
{
for (sName in oParams) {
if (sUrl.indexOf("?") > -1) {
sUrl += "&"; } else { sUrl += "?";
}
sUrl += encodeURIComponent(sName) + "=" + encodeURIComponent(oParams[sName]); }
var oScript = document.createElement("script"); oScript.src = sUrl; document.body.appendChild(oScript);
}
This function expects to be passed a URL for a JavaScript file and an object containing query string arguments. The query string is constructed inside of the function by iterating over the properties of this object. Then, the familiar dynamic script loading technique is used. This function can be called as follows:

var oParams = { "param1": "value1", "param2": "value2"};
makeRequest("/path/to/myjs.php", oParams)

Next, you need a way to assign the callback function to be used. It's quite possible that you'll want to access the same information on different pages and in different ways. Forcing each page to have a callback function named "callback" isn't very good architectural design. Instead, it would be better to tell the JavaScript file the name of the callback function to use so that it can be dynamically inserted. The name of the function can be passed as another parameter for the query string:

var oParams = { "param1": "value1", "param2": "value2", "callback": "myCallbackFunc"};
makeRequest("/path/to/myjs.php", oParams);

The file creating the JavaScript then has to take the name of the callback function and output it into the code, as below:

var sMessage = "Hello world!";
(sMessage);

The first part of this file sets the content type to text/javascript so that the browser recognizes it as JavaScript (though many browsers don't check the content type of files loaded using ) Next, a JavaScript variable called sMessage is defined as a string, "Hello world!". The last line outputs the name of the callback function that was passed through the query string, followed by parentheses enclosing sMessage, effectively making it a function call. If all works as planned, the last line becomes:
myCallbackFunc(sMessage);

Example 2
This example builds upon the previous one, but this time, you're going to send some additional information to the server and tell it which callback function to call. First, take a look at the PHP file that will be outputting the JavaScript:

var sMessage = "Hello, ";var sName = "
";
(sMessage + sName);

The JavaScript that will be output defines two variables, sMessage and sName; the former is filled with "Hello, ", the latter is assigned the value of the name parameter in the query string. Then, the name of the callback function is out, passing in the concatenation of sMessage and sName (combining server-side data with data passed from the client).
On the client side, the web page contains a textbox and a button:

html>

Example 2
// -1) {
sUrl += "&";
} else {
sUrl += "?";
}
sUrl += encodeURIComponent(sName) + "=" + encodeURIComponent(oParams[sName]);
}
var oScript = document.createElement("script");
oScript.src = sUrl;
document.body.appendChild(oScript);
}
function messageFromServer(sText) {
alert("Loaded from file: " + sText);
}
function getInfo() {
var oParams = {
"name": document.getElementById("txtInput").value, "callback": "messageFromServer"
};
makeRequest("example2js.php", oParams);
} //]]>


When the button is clicked, the getInfo() method is called, which loads an object with a name parameter (taken from the textbox) and a callback parameter. Then, the makeRequest() function is called, passing in these values. After the script has been loaded, the messageFromServer() function will be called, popping up a message displaying what was received from the server

Drawbacks
Though dynamic script loading is a quick and easy way to establish client-server communication, it does have some drawbacks. For one, there is no feedback once the communication is initiated. If, for example, the file you are accessing doesn't exist, there is no way for you to receive a 404 error from the server. Your site or application may sit, waiting, because the callback function was never called.
Also, you can't send a POST request using this technique, only a GET, which limits the amount of data that you can send. This could also be a security issue: make sure you don't send confidential information such as passwords using dynamic script loading, as this information can easily be picked up from the query string.

smrithi
I love the technology AJAX and started writing blog on the same. You can check it out at Ajax. I am an optimist who believes that every person has a purpose to come to this world. And getting inspired helps to move at a good pace in life and I love writing inspiring stories and are available at Touching Stories
Rate this Article: 0 / 5 stars - 0 vote(s)
Print Email Re-Publish

Add new Comment



Captcha

  • Latest Information Technology Articles
  • More from smrithi

Boost your Business with the Right Website Design Services

By: Giorgio Colombo | 12/11/2009
There is no doubt about the role that websites play in the growth of any business. Every business requires it to attract more and more clients. It also helps in expanding the business to overseas markets. This article will help you by giving some tips, which will help in making your site better and attractive for clients.

Long Term Rentals In Javea

By: Lewis Cline | 12/11/2009
When hiring holiday apartments in Spain or long lets in javea, the length of stay usually has effects on the cost. Often the price is less the longer the rental period. When you book direct with the proprietor a studio to lease in Torrevieja Spain or for a long term let in javea it is a very reasonably priced option as opposed to staying in a hotel. You need to be careful where you look for a villa for hire in...

70-680 practice test

By: calomi | 12/11/2009
This preparation kit also contains study notes, 70-680 pdf, 70-680 download, 70-680 practice test and 70-680 review.

Microsoft 83-640 exam -Testinside

By: calomi | 12/11/2009
All of our practice exams including the 83-640 exam will prepare you for success. 83-640 is a challenging exam, with Testinside 83-640 study guide, you can feel safe with our question and answers that will help you in obtaining your successful completion of your 83-640 exam.

Testinside 9A0-096 exam

By: calomi | 12/11/2009
The 9A0-096 TestInside practice exams and study guides are current and updated monthly, providing you with the highest Adobe 9A0-096 TestInside ROI. Start you road to Adobe 9A0-096 TestInside success today, buy purchasing the TestInside 9A0-096 training materials today!

The Revolutionary R4i Cards

By: perrykelly | 12/11/2009
The days of juggling with the different cartridges to play different games are over. The modern times are ruled by the revolutionary r4 I cards. This is the latest version of the r4 ds cards.

What a Colocation Data Center in Palm Beach County, Miami-Dade County and Broward County Does

By: Business Local Listings | 12/11/2009
A colocation data center’s services are known as full cloud computing services. This means offering several different business applications which its clients can access online even as the associated software and data are stored securely in virtual servers in a remote location. These services include dedicated virtual servers, managed dedicated colocation hosting, private networks, internet network security, data storage and data backup, broadband internet access, and VoIP.

Project Delivery On-Demand- an innovative model to streamline IT processes

By: Ripple Effect | 12/11/2009
PPM changes how organizations approve, plan and deliver projects. From a bottom line perspective, PPM enables organizations to improve their return on project investments. Project Consulting Group (PCG), a Minneapolis-based project and portfolio management consulting firms, is helping companies today to adopt an innovative model to streamline IT processes called Project Delivery On-Demand. PCG provides a proven framework for assimilating people, process, technology and culture.

Silent Love Story

By: smrithi | 05/02/2008 | Relationships
This is a silent love story of how two hearts depart from each other and at last, their love towards each other helps them to live with each other forever

Introduction to Ajax

By: smrithi | 31/12/2007 | Information Technology
This article gives you an overview of what AJAX is and its use in the real world. Enjoy AJAXING!!

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.52, 8, w1)