Making AJAX generated pages search engine friendly
After several hours of tedious experimentation and research, I’ve finally come up with a solution to the common conundrum: how can pages be loaded via AJAX calls while still making this content accessible to the search engines?
The most common solution to this problem has usually and traditionally been one of the following:
- Create separate pages for Javascript/AJAX compatible browsers and for the search engines
- Don’t use AJAX at all, negotiate your content the old fashioned way - even if this results in a less user-friendly experience for your visitors
- Use AJAX, accept the fact that your pages will not be crawlable by the search engines
For those of you that have played around with my media player on the NetMusician website you will have noticed that the beauty of this player is that the audio doesn’t cut in and out between page loads. Audio players that do this are a peeve of mine (especially ones that force audio on you with no way to turn them off!), I just think this is poor design.
The most common solutions to circumvent this problem have been:
- Put the media player in a pop-up window
- Put the media player in a frame
- Load the content surrounding the media player as needed via an AJAX trigger
While solution #1 is often a workable solution, I think it is often rather awkward. Frames are a pain in the butt in general, and visitors can’t save direct URLs to the page they are on. AJAX is the nicest way to handle this, IMHO, but the search engine issue has always been a challenge.
Enter my Javascript function…
This function will read all of the HTML located within a div tag and will search for HTML hyperlinks. Internal, relative links are replaced with a Javascript link such as: javascript:ajax(’mypage’), external links and absolute links are left as is.
The result is that as long as you use internal links within your site, these can be converted into your AJAX/Javascript triggers on-the-fly via a Javascript onload event handler. For all non Javascript compatible browsers, this event handler will simply be ignored, leaving all of your links intact.
I believe this to be far easier than probing user agents and providing a separate set of links on the page for certain web browser user agents (or creating separate versions of the page). This makes it difficult to create links via a CMS, and is limited to the search engines you have provided user agent conditional statements for. This link rewriting technique, I believe, is a far more simplistic and effective approach.
I’ve uploaded a demo of this technique in action. Please leave a comment here if this is of use to you or if you have any questions… Enjoy!
Edit: modified function to work in IE