spacer

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

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

Java Developer
Professional Technical Resources
US-WA-Seattle

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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]

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

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Crucial Triples Up With New Three-Channel DDR3 Kits · Meet the Finalists: Excellence in Technology Awards · Tealeaf Offers Insight to Mobile Customer Behavior

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

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