Unique URL Pattern in Ajax Web Application
Testing: Can I really edit this?
Background
Few days ago one of my friend asked me how does Gmail change its URL while user operates inside it without page refreshing, I've no idea about that at that time, he then shared a link Ajax Pattern - Unique URLs which deep dives into this topic, as the article mentioned: Unique URL make your website's link "Bookmarkable, Linkable, Type-In-Able", plus Sharable IMHO, easy to be shared to Social network which is extremely important nowadays.Implementation
The key technology to achieve the ""Unique URL" goal could be summarized into two points:- If the content in the page has been updated by Ajax significantly enough, update the URL (
location.hash
) as well. - Every time the Ajax page loads, JS should understand the URL and render related content as well.
// Ajax rendering all blog entries in page number 5 location.hash = 'Blogs&Page5';
<body onload="restoreAjaxContent()"> <script type="text/javascript"> function restoreAjaxContent(){ var urlHash = location.hash; var curPageNo = urlHash.replace('Blogs&Page',''); // Safe parse curPageNo into number, handles wrong parameters (Ignored for here) // Display loading text/image on the page (optional but better UX). // Ajax rendering all blog entries in page number curPageNo. } </script> </body>
Advantage
-
Better user experience.
Every time user accesses the unique URL Ajax page, the fixed part of this page loaded first, and then loads the main content asynchronously, if the main content is large enough, for example, contains images or rich media content, the "async loading" is much better than the page loading blocked by downloading those images/medias. For instance, originally loading one specific page requires 2 seconds totally, after applying the this "async loading", the fixed part cost 0.4 seconds to be loaded and the main content costs 1.8 seconds, from user's point of view, usually the latter case would be better because the user see your page is partially loaded within a short period (0.4) which feels good enough, plus a graceful loading/splash screen, eventually the UX got improved significantly!
-
Better SEO support (Performance Aspect)
The page rendering speed is an important fact for a search engine's crawler, by applying "async loading", the crawler will deem the page it is crawling has a good loading speed - 0.4 seconds.
-
Easy to support W3 web standard
This is sort of kidding:) Since your main content is Ajax loaded, W3 validator (so does Search Engine) won't validate the main content which is very possible does not strictly adhere all the standard rules.
-
Main content cannot be indexed by Search engines
All main content is loaded by JavaScript, Search engine won't crawler those content, this is a serious problem! However, it is easy to walk around, builds a traditional page without Ajax, store it into sitemap.xml and submit to search engine Web Master tool.
-
Harder to development and to maintain
Client JavaScript/Ajax development is more complex and less convenient comparing to server side technology like ASP.NET, JAVA EE or PHP. Although there are jQuery (write less, do more), Prototype.js (make develop JS in a more OO way), DoJo and so on, it still might not be very happy while a developer is struggling with mixed HTML/CSS/JavaScript:)
Leave a comment