spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / ajax_tech2 / 1 current pageTo page 2
[next]

ASP 3.0/.NET Developer
Jupitermedia
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

Alternate Ajax Techniques, Part 2

By Nicholas C. Zakas.

In the last installment of this series, you learned about dynamic script loading, which involves creating a <script/> element dynamically and using it load JavaScript from the server. To make that work, your browser had to be DOM-compliant. In Part 2, you'll learn how to use images and cookies to enable client-server communication even on older browsers that don't support the DOM.

Images and Cookies

When people think about cookies nowadays, most think about security concerns, spyware and evil corporations tracking their every move. Those fears are understandable given what goes on in the world of the Web, but cookies really are just small pieces of data that can be accessed by both the client (through JavaScript) and the server. However, there are several restrictions placed on cookies that you need to be aware of before using them:

  • Each domain can store a maximum of 20 cookies on a user's machine. It's best to reuse cookies whenever possible instead of creating new ones.
  • The total size of the cookie (including name, equals sign, and value) cannot exceed 4096 bytes, meaning cookies are useful for storing small amounts of data only.
  • The total number of cookies allowed on a machine is 300.
Each request to and response from the server contains cookie information, regardless of what type of resource is being requested. This is where images come in.

Since Netscape Navigator 3, you've been able to change the src attribute of an image using JavaScript. This counts as a request and, therefore, brings with it cookie information from the server. As it turns out, images also fire a load event when the image has completely finished loading, allowing you to know exactly when the data you requested is ready. This makes images an ideal way to manipulate cookies for client-server communication.

The Technique

The basic technique behind using images and cookies is similar to preloading images. You need to create a <img/> element and then assign its src attribute. To tell when the image is loaded, assign an onload event handler:

    var oImg = document.createElement("img");
    oImg.onload = function () {
      alert("Image is ready");
    }
    oImg.src = "/path/to/myimage.gif";
Unlike dynamically creating a <script/> element, the download of an image begins as soon as the src attribute is assigned, meaning that you don't even need to add the image to the page. In fact, you don't even need to use an <img/> element at all, you can use the Image object:

    var oImg = new Image();
    oImg.onload = function () {
      alert("Image is ready");
    }
    oImg.src = "/path/to/myimage.gif";
Of course, this is just part of the process. If the image request was meant to be bring back data in a cookie, then you'll need to read that data in the onload event handler. The getCookie() method gives you easy access to specific cookie values:

    function getCookie(sName) {
      var sRE = "(?:; )?" + sName + "=([^;]*);?";
      var oRE = new RegExp(sRE);

      if (oRE.test(document.cookie)) {
        return decodeURIComponent(RegExp["$1"]);
      } else {
        return null;
      }
    }
Without going too deeply into this code, document.cookie returns a series of name-value pairs that are URL-encoded and separated by semicolons. This function looks through the string for the value matching the given name.

home / programming / ajax_tech / 1 current pageTo page 2
[next]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes

Created: March 27, 2003
Revised: March 17, 2006

URL: http://webreference.com/programming/ajax_tech2/1