• Home
  • Services
  • Profile
  • Portfolio
  • Contact
  • Links
  • Blog
  • Archives
  • Sitemap
Subscribe: Posts | Comments | E-mail
  • Ajax XMLHttpRequestAsynchronous HTTP Request How To's
  • FreebiesBest Freebies Collection
  • MiscellaneousNews, Announcements & General Stuffs
  • Print & Web DesignPhotoshop, Vector, CSS, XHTML
  • ProgrammingProgramming PHP, ASP, .NET and more

Bali Web Design

Posted on September 30, 2008 - by webmaster

Ajax against SEO and Web Accessibility

Ajax XMLHttpRequest Freebies Tutorials Web 2.0

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:

  1. First visited page either from direct access or referer link including from search engine result
  2. Search Engine Bot Agent
  3. Screen readers with no javascript enabled
  4. 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

Share this article:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • MySpace
  • Ping.fm
  • Pownce
  • Slashdot
  • StumbleUpon
  • Technorati
  • Tumblr
  • TwitThis
This entry was posted on Tuesday, September 30th, 2008 at 01:02 and is filed under Ajax XMLHttpRequest, Freebies, Tutorials, Web 2.0. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

22 Comments

We'd love to hear yours!



  1. Visit My Website

    October 3, 2008

    Permalink

    Ajax Jquery Tutorial: ajaxify your web pages said:

    [...] 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! [...]



  2. Visit My Website

    January 12, 2009

    Permalink

    David said:

    I’m having a little trouble following the first example, could you please provide the source for the demo page?

    Thanks!



  3. Visit My Website

    March 9, 2009

    Permalink

    Redesign My Official Website « Bali Web Design said:

    [...] SEO friendly and Accessible ajax [...]



  4. Visit My Website

    April 8, 2009

    Permalink

    Ajax Jquery Tutorial: ajaxify your web pages said:

    [...] 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 [...]



  5. Visit My Website

    April 12, 2009

    Permalink

    Rik said:

    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!



  6. Visit My Website

    April 13, 2009

    Permalink

    webmaster said:

    they should be working fine now



  7. Visit My Website

    April 23, 2009

    Permalink

    Chris said:

    Hey. Could I get your source for the PHP version of it?

    - Chris



  8. Visit My Website

    May 27, 2009

    Permalink

    Pascal said:

    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.



  9. Visit My Website

    June 19, 2009

    Permalink

    joseph said:

    what are the uses of the params @ function(d,s) and function(o,s,e), are they necessary?

    nice tutorial…



  10. Visit My Website

    June 26, 2009

    Permalink

    kthxbai2u said:

    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…



  11. Visit My Website

    June 26, 2009

    Permalink

    kthxbai2u said:

    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?



  12. Visit My Website

    July 14, 2009

    Permalink

    Alberto Jauregui said:

    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?



  13. Visit My Website

    July 22, 2009

    Permalink

    sophia said:

    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



  14. Visit My Website

    July 22, 2009

    Permalink

    SEO Company said:

    Is there a source for a php version.



  15. Visit My Website

    August 13, 2009

    Permalink

    Philip said:

    This is AWESOME! thanx man, perfect fix for my problems.



  16. Visit My Website

    October 11, 2009

    Permalink

    Katrina Kaif said:

    Great Article but is there any version for c# and ASP.net?



  17. Visit My Website

    November 1, 2009

    Permalink

    carlo said:

    very simple, clear and so helpful
    thanks a lot :)



  18. Visit My Website

    March 8, 2010

    Permalink

    Todd said:

    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);
    });
    });



  19. Visit My Website

    March 30, 2010

    Permalink

    annoyed of IE said:

    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?



  20. Visit My Website

    April 16, 2010

    Permalink

    Baker said:

    very informative article, could be more nice if we could a php version for this.



  21. Visit My Website

    August 24, 2010

    Permalink

    Silvio said:

    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



  22. Visit My Website

    August 26, 2010

    Permalink

    seagreen7 said:

    Good!



Leave a Comment

Here's your chance to speak.

  1. Name (required)

    Mail (required)

    Website

    Message

  • Most Popular Posts

    • { 108 } ResponsesPHP ajax login form using Jquery
    • { 79 } Responsesphp ajax tutorial create ajax based login form using jquery
    • { 71 } ResponsesASP Classic Programming Still Alive
    • { 53 } Responses63+ best practice to optimize PHP code performances
    • { 44 } ResponsesVideobox: Lightbox for videos
  • Latest Posts

    • PHP Newbie: Proper handling of Error & Exception in PHP
    • Why SEO lost importance
    • jQuery Mobile Announced : Touch-Optimized Web Framework for Smartphones & Tablets
    • Multiple Google Account Signin
    • Install, Configure & Running Memcache in Windows as Service
    • DynamicWP Image Cube Wordpress Plugin
    • New Wordpress 3.0 API menu_page_url
    • Wordpress 3.0 Custom Taxonomy
    • JQuery DataGrid Plugin Compass
    • Upgrading to upcoming CodeIgniter 2.0
  • Categories

    • advertorial
    • Ajax XMLHttpRequest
    • Featured
    • Freebies
    • Miscellaneous
    • Print & Web Design
    • Programming
    • Tutorials
    • Web 2.0
  • Featured News

    • PHP Newbie: Proper handling of Error & Exception in PHP by webmaster on August 18, 2010
    • Why SEO lost importance by webmaster on August 16, 2010
    • jQuery Mobile Announced : Touch-Optimized Web Framework for Smartphones & Tablets by webmaster on August 16, 2010
    • Install, Configure & Running Memcache in Windows as Service by webmaster on June 27, 2010
    • Website Optimization Best Practices, speed up your website load by ruby on May 21, 2009
  • Tag Cloud

    • Actionscript advertorial Ajax XMLHttpRequest API best practices CakePHP chat Classic ASP CMS CSS Development facebook firefox Flash flickr Freebies gmail HTML icons Javascript Jobs jQuery jQuery Plugins Library Lightbox memcache Mootools news Personal PHP PHP Frameworks plurk Print & Web Design prism Prototype regex regular expression Scriptaculous SEO Tutorial twitter Web 2.0 Wordpress Works YUI
  • Archives

    • August 2010
    • June 2010
    • May 2010
    • September 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
copyleft 2007 - 2010 Bali Website Design Studio