• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Deep Linking in JavaScript and Ajax Applications ...

Last week I posted a tutorial that demonstrated using a simple application how to implement progressive enhancement into your Ajax projects. The one major flaw in the final Ajax-driven page from that tutorial is the lack of deep linking when JavaScript is enabled.

Although the resulting code is clean, works well, and is easy to maintain, the lack of deep linking is enough to cause a client to balk at the use of progressive enhancement in such a circumstance. So, in this brief tutorial, I’ll describe how to incorporate deep linking into that page.

If you haven’t already gone through the previous tutorial, doing so might help you get up to speed — but it’s not absolutely necessary, since the code we’ll be using is pretty straightforward.


Step 1: Review the Primary Function From the Previous Tutorial

The final JavaScript code from the previous tutorial included a function called getEmployeeInfo, which is the primary piece of code we’ll be working with in this tutorial. Here is that function:


   1. function getEmployeeInfo() { 
   2.  
   3.     var myLinksCollection = document.getElementsByTagName("a"); 
   4.  
   5.     for (i=0;i<myLinksCollection.length;i++) { 
   6.         myLinksCollection[i].onclick = function() { 
   7.             if (this.href.indexOf("view=") !== -1) { 
   8.                 var clickedHREF = this.href; 
   9.                 var clickedView = clickedHREF.split("view="); 
  10.                 ajaxInitiate(clickedView[1]+.html); 
  11.                 return false; 
  12.             } 
  13.         } 
  14.     } 
  15.  
  16. } 

function getEmployeeInfo() {

 var myLinksCollection = document.getElementsByTagName("a");

 for (i=0;i<myLinksCollection.length;i++) {
  myLinksCollection[i].onclick = function() {
   if (this.href.indexOf("view=") !== -1) {
    var clickedHREF = this.href;
    var clickedView = clickedHREF.split("view=");
    ajaxInitiate(clickedView[1]+.html);
    return false;
   }
  }
 }

}

We’ll be adding a few lines of code to the above function in order to implement deep linking into this application.


Step 2: Add the Hash Character to the URL

In order to deep link to a particular state in the application, we want to utilize the hash character (#), using it to append an identifier to the URL of the page. JavaScript lets us access the current hash using the following line of code:
view plaincopy to clipboardprint?

   1. location.hash = "identifier"; 

location.hash = "identifier";

That line can be added anywhere inside the anonymous function, and that will add a hash to the current URL, followed by the word “identifier”, without reloading the page. If a hash already exists, the text following the hash will be changed to “identifier”. The hash character makes the link internal, preventing a page refresh, the same way this would occur in an href value of an anchor tag. Of course, we don’t want to just put any old text in there, but place some text that will help us identify the state of the Ajax application.

In the anonymous function, the current state is identified by the clickedView array, so we can use that to append an identifier to the current URL, like this:

   1. location.hash = clickedView[1]; 

location.hash = clickedView[1];

Let’s take a look at how the page works after adding that line to the code:

Demo Page #1

Now the content is loading correctly, and each time a link is clicked, the value following the hash character in the URL is changed, helping us identify what state the application is in.


Step 3: Change Content According to the Current State

Now that the application is appending a state identifier to the current URL, we need to check to see if that identifier exists, and display the correct information. Here’s the code that will accomplish this:

   1. if (location.hash) { 
   2.         ajaxInitiate(location.hash.replace("#","")+.html); 
   3.     } 

if (location.hash) {
  ajaxInitiate(location.hash.replace("#","")+.html);
 }

It’s pretty simple: We check to see if the “hash” character exists. If it does, then we call the ajaxInitiate function with the hash text as the file name that’s passed as an argument. Since the location.hash property includes the actual hash symbol (#), we’re using the replace method to strip out the hash and get a clean file name.

The above code is added before the loop in the getEmployeeInfo function, so the complete function now looks like this:


   1. function getEmployeeInfo() { 
   2.  
   3.     if (location.hash) { 
   4.         ajaxInitiate(location.hash.replace("#","")+.html); 
   5.     } 
   6.  
   7.     var myLinksCollection = document.getElementsByTagName("a"); 
   8.  
   9.     for (i=0;i<myLinksCollection.length;i++) { 
  10.         myLinksCollection[i].onclick = function() { 
  11.             if (this.href.indexOf("view=") !== -1) { 
  12.                 var clickedHREF = this.href; 
  13.                 var clickedView = clickedHREF.split("view="); 
  14.                 ajaxInitiate(clickedView[1]+.html); 
  15.                 location.hash = clickedView[1]; 
  16.                 return false; 
  17.             } 
  18.         } 
  19.     } 
  20.  
  21. } 

function getEmployeeInfo() {

 if (location.hash) {
  ajaxInitiate(location.hash.replace("#","")+.html);
 }

 var myLinksCollection = document.getElementsByTagName("a");

 for (i=0;i<myLinksCollection.length;i++) {
  myLinksCollection[i].onclick = function() {
   if (this.href.indexOf("view=") !== -1) {
    var clickedHREF = this.href;
    var clickedView = clickedHREF.split("view=");
    ajaxInitiate(clickedView[1]+.html);
    location.hash = clickedView[1];
    return false;
   }
  }
 }

}

Drawbacks to this Method

There are two drawbacks to this method. One small drawback is that the content does not change if the URL is manually altered after the initial page view. The hash character will only affect the page’s content on the first page view, when the entire page is initially loaded. But that’s not a big deal, since the content can easily be changed by clicking any of the links after the initial page view. The only time we’re really concerned about the state of the hash tag is on the initial page load, so it works fine in that respect.

The other drawback is that this method does not preserve the back button. You’ll notice that if you use the back button to try to visit a previous state, the URL will change, but the content is not affected. This happens because the URL change is only internal, and does not initiate a full page refresh. Preserving the back button in Ajax applications is a more complicated process that is beyond the scope of this simple tutorial.


Conclusion

And that’s it, our application now has deep linking, and works in virtually the same way as the PHP-only version, aside from the drawbacks mentioned above.

Use the links below to view the final demo page, or download the complete code, including the code from the previous tutorial. The link to the demo page includes a hash identifier, so you can see how the code works.

View Demo

Download Code

 Original Source:
http://www.impressivewebs.com/deep-linking-javascript-ajax/

AddThis Social Bookmark Button

Posted at 11:29:18 am | Permalink | Posted in Javascript  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

e-messenger

MSN Web Messenger

eBuddy

ASP.NET Ajax CalendarExtender and Validation

AIM Express

Ajax Tools for ASP.NET Developers



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (176)
  • Ajax (112)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (84)
  • Email (23)
  • Facebook (84)
  • Flash (20)
  • Google (54)
  • Html (29)
  • Image (12)
  • International Calls & VOIP (7)
  • Java (58)
  • Javascript (280)
  • jQuery (200)
  • JSON (75)
  • Perl (2)
  • PHP (172)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (32)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (227)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (29)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange