Posted on September 30, 2008 - by webmaster
Ajax against SEO and Web Accessibility
Ajax has become a new hot terms in web development world, since the rapid growth popularity of some pioneer ajax based web application like Gmail and Google Maps. Lots of web developers, web designers and internet users very excited with the more possibilities which can be done by ajax, many websites developed using the advantages of ajax technique, and since then lots of ready to use ajax toolkit/framework available for developers to simplify the experience of ajax integration eg: jquery, prototype, mootools, GWT, Dojo etc.
But there also some cons in using ajax, and one of the hottest is ajax against SEO & Web Accessibility, Just as ajax, SEO (search engine optimization) also been playing more and more role in web development world, SEO do the self marketing of website through search engines, while ajax providing more interactivity between the user and the web page. Untill now Search Engines Robots do not parse javascript means Search Engines wont crawled content delivered by ajax, web accessibility? since ajax rely on javascript it will break a fall on the screen readers which did not support javascript or javascript has been disabled on it.
Then how to mix ‘em? well i am not an expertise in SEO (Search Engine Optimization) neither does web accessibility, but as much as i read about SEO and web accessibility, it is not as complicated as some said about it. Here i will show you how to create accessible and SEO friendly ajax using Jquery, since i am more familiar with it rather than other toolkits, but ofcourse this technique can be used with other javascript frameworks as well.
- Create the wholepage as usual using CSS + xHTML. Web standards compatible? well thats not a law, just recommendations, but its good to follow em
- Optimize your pages as much as you feel it good
- !important Add filtering in your server side codes where the page is rendered to identify the request from normal HTTP request or via ajax by passing a parameter through ajax request, here i assumed the parameter name is ajax, so whenever var ajax has value then render only any content that needed without any html, head, body tags, else render the full page included those neccessary html tags
//@ PHP
if(!empty($_POST['ajax'])) {
//@ Render only neccessary content
} else {
//@ Render the whole page
}
'@ ASP
if Len(Request.Form("ajax"))>0 then
'@ Render only neccessary content
else
'@ Render the whole page
end if
Assumed we have page structures as follow (you can use apache rewrite or IIS rewrite module to make the links more SEO friendly):
<div id="nav"> <ul> <li><a href="" title="Welcome Home">home</a></li> <li><a href="" title="About Me">about</a></li> <li><a href="" title="My Services">services</a></li> </ul> </div> <div id="wrapper"> <!-- CONTENT GOES HERE --> </div>
Search Engine Bots will not have a problem to crawl ‘em and they will follow and probably crawl the page based on the “href” attribute value.
Now we add the ajax functionality on each links. Download latest jquery library or used Google ajax library API, then create a .js file, name it ajax.js, then add these codes between the head tags
<script type="text/javascript" src="path/to/jquery.js"></script> <script type="text/javascript" src="path/to/ajax.js"></script>
Modify the ajax.js like follow:
$(function(){
var LoadMsg = 'Please Wait ...';
$('#nav ul li a').click(function(){
if(!$(this).hasClass("current")) {
var _Href = $(this).attr('href');
$('<div id="loading">'+LoadMsg+'</div>').appendTo('body').fadeIn('slow',function(){
$.ajax({
type: 'POST',
url: _Href,
data: "ajax=1",
dataType: 'html',
timeout: 5000,
success: function(d,s){
$('#loading').fadeOut('slow',function(){
$(this).remove();
$('#wrapper').slideUp('slow',function(){
$(this).html(d).slideDown('slow');
});
});
},
error: function(o,s,e){
window.location = _Href;
}
});
});
}
return false;
});
});
Just that
, now test your pages, try to disable/enable javascript and see how it works.
- Search engine bots will crawled your content as normal, because each link has written in semantic html format
- Any screen readers which has limitation in parsing javascript also will able to load your content
- What if javascript has enabled and the ajax request return error? well take a look at the ajax error handling, we simply tell the javascript to redirect the page using normal HTTP request
Do you want to see an online demo page? well here it is.
Update
My web pages were built using fully HTML, how can i adopt this approach?
If your web pages were built using fully HTML with no server side codes (ASP, PHP etc.), to adopt this approach you need to create double pages for each of them. 1 page designed with all neccessary HTML tags as a valid HTML page, including CSS and Javascript, and the other one contains only neccessary content.
. webroot/ ... index.html ... about.html ... services.html ... style.css ... jquery.js ... ajax.js ... pages/ ... ... index.html ... ... about.html ... ... services.html
And those pages in the root directory will be reached within these conditions:
- First visited page either from direct access or referer link including from search engine result
- Search Engine Bot Agent
- Screen readers with no javascript enabled
- ajax load failed or timeout
the structure could be looked like this:
<html>
<head>
<title>Page Title</title>
<meta name="keywords" content="any keywords" />
<meta name="description" content="any descriptions" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<link href="style.css" rel="stylesheet" media="all" />
</head>
<body>
<div id="nav">
<ul>
<li><a href="index.html" class="current">Home</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="about.html">About me</a></li>
</ul>
</div>
<div id="wrapper">
<!-- CONTENT TEXT GOES HERE! -->
</div>
<div id="footer">
<!-- FOOTER TEXT GOES HERE! -->
</div>
</body>
</html>
Web pages inside /pages/ will only accessed via ajax in screen readers with javascript enabled. It only contain Informational data.
Modify ajax.js and replace the content with these codes:
$(function(){
var LoadMsg = 'Please Wait ...';
var AjaxPath = 'pages/';
$('#nav ul li a').click(function(){
if(!$(this).hasClass("current")) {
var _Href = $(this).attr('href');
$('#nav ul li a').removeClass("current");
$(this).addClass("current");
$('<div id="loading">'+LoadMsg+'</div>').appendTo('body').fadeIn('slow',function(){
$.ajax({
type: 'GET',
url: AjaxPath+_Href,
dataType: 'html',
timeout: 5000,
success: function(d,s){
$('#loading').fadeOut('slow',function(){
$(this).remove();
$('#wrapper').slideUp('slow',function(){
$(this).html(d).slideDown('slow');
});
});
},
error: function(o,s,e){
window.location=_Href;
}
});
});
}
return false;
});
});
!important protect files inside /pages/ directory from being indexed by search engine using robots.txt
Sample of implementation here

Visit My Website
October 3, 2008
Permalink
[...] index.html, tell the browser to load the homepage.html via ajax !update better approach available here, prevent a fall on screen readers without javascript support also SEO compliants Bookmark This! [...]
Visit My Website
January 12, 2009
Permalink
I’m having a little trouble following the first example, could you please provide the source for the demo page?
Thanks!
Visit My Website
March 9, 2009
Permalink
[...] SEO friendly and Accessible ajax [...]
Visit My Website
April 8, 2009
Permalink
[...] index.html, tell the browser to load the homepage.html via ajax !update better approach available here, prevent a fall on screen readers without javascript support also SEO compliants Share this [...]
Visit My Website
April 12, 2009
Permalink
Hey, thanks for the tutorial, but both the examples don’t use the jquery/ajax, even when I have it enabled.
If it helps: I use ff 3.0.8.
Thanks!
Visit My Website
April 13, 2009
Permalink
they should be working fine now
Visit My Website
April 23, 2009
Permalink
Hey. Could I get your source for the PHP version of it?
- Chris
Visit My Website
May 27, 2009
Permalink
When using “full” HTML pages, could there be a way to only read out the content of a certain DIV?
If I always had the DIV #wrapper on every page, could I only replace it’s content?
That way you wouldn’t have to duplicate the content in order to have the website SEO friendly.
Visit My Website
June 19, 2009
Permalink
what are the uses of the params @ function(d,s) and function(o,s,e), are they necessary?
nice tutorial…
Visit My Website
June 26, 2009
Permalink
What if you want to keep a PHP session open during this semi post / ajax request? If the entire site is loaded with ajax, there is no post for the entire page at least until the user leaves the site. This would time out the session…
Visit My Website
June 26, 2009
Permalink
Maybe it would work to take your expire time cookie, and add time to it manually each ajax request? Would it be possible to change cookies and session variables after the headers are sent?
Visit My Website
July 14, 2009
Permalink
I have a little problem, I have different pages in full HTML, but when i try to load a page with ajax in wich I use some effects, the effects don’t work on that page loaded by ajax.
For example, my gallery.html has some effects on jquery using transition between images, so, when I load gallery.html with ajax, the effects don’t work, any suggestions?
Visit My Website
July 22, 2009
Permalink
I highly appreciate if you can give us the source code of PHP files. I’m wondering what code looks like in “@ Render only neccessary content” part
many thanks
Visit My Website
July 22, 2009
Permalink
Is there a source for a php version.
Visit My Website
August 13, 2009
Permalink
This is AWESOME! thanx man, perfect fix for my problems.
Visit My Website
October 11, 2009
Permalink
Great Article but is there any version for c# and ASP.net?
Visit My Website
November 1, 2009
Permalink
very simple, clear and so helpful
thanks a lot
Visit My Website
March 8, 2010
Permalink
I’m new to jquery and while has used javascript and the xmlhttpRequest with success. I seem to be hung up on passing a variable as data like “?q+str” along with the url. Can anyone help the new Guy.
Thanks
Todd
$(document).ready(function(){
var str = ‘17′;
$(“#b01″).click(function(){
htmlobj=$.ajax({url:”RecipesTable.php”,async:false,Data:”?q”+str});
$(“#test”).html(htmlobj.responseText);
});
$(“#b02″).click(function(){
htmlobj=$.ajax({url:”getlist.php5″,async:false});
$(“#test”).html(htmlobj.responseText);
});
});
Visit My Website
March 30, 2010
Permalink
It makes me tired to read all these posts to that topic over and over… and if you test it in IE it doesn´t work.
WHY?
Visit My Website
April 16, 2010
Permalink
very informative article, could be more nice if we could a php version for this.
Visit My Website
August 24, 2010
Permalink
Nice tutorial man! I was searching around on the web as I’m facing this problem at this very moment. I’m using MasterPages and I’d like to know where you put the server script (the ASP one) and when you call it? It’s not going to run without calling it on a way or another… right? I think I’m missing something. Thanks for your help.
Btw: Your pages don’t load with firefox when I disable all Javascripts. U guys gave that too?
Kind regards
Silvio
Visit My Website
August 26, 2010
Permalink
Good!