Google + Ajax = Troubles

Internet Add comments

InternetWhen visiting a website based on Ajax, you won’t like any other sites based on ‘old’ technology. But is Ajax as good as the website visitors say. If you ask a web developer, like myself, he will probably answer: “The idea is great, but it brings a lot of troubles with it, so I won’t use it very much.”

But why won’t he use it much if he knows that visitors like it?

As a web developer you want your website to be accessed by everyone on the internet, especially by searchbots like Google. To do this you have to keep in mind that the googlebot has some limits, one of them is that for security reasons the googlebot won’t run client-side scripts like JavaScript. The googlebot uses a so called ‘text browser’ like Lynx

As a web developer in some cases we use Javascript to make the website more interactive, mostly these interactivities aren’t very interesting for the googlebot and the web developer doesn’t mind that it won’t be indexed.

Ajax is a quit old technology which is now going to be popular. Thanks to Ajax the page you are on won’t have to be completely reloaded, but parts of it can be changed. For more information on Ajax go to this page

Google BoardSo Ajax is totally based on JavaScript, in some cases it isn’t a problem, like in a webbased mailclient, because these pages don’t have to be indexed. But if you use Ajax on a normal website which you like to be indexed by Google, there you have the major problem for web developers!

In my opinion, problems exist to be solved, so I started to do some research.  And I found this:

<form action="something.php" onsubmit="return checkscript()">

If the function checkscript() return ‘True’ it follows something.php and if it returns ‘Fals’, it won’t. What we want is that Google sees a URL and follows it, but users with Javascript enabled (almost everyone (about 99.99%)) won’t follow this URL but follow the Ajax function. It may be easier to just put here the code, so here it is.

With this code you can show the same page to people with Javascript enabled and disabled and it is easily accessibly by the googlebot (or any search engine) for indexing.

<?php
if (isset($_GET['page'])) {
if (isset($_GET['ajax'])) {
include($_GET['page'] . “.txt”);
} else {
?>
<html>
<head>
<title>Dit is a test</title>
<script type=”text/javascript”>
function stateChanged()
{
switch(xmlHttp.readyState)
{
case 4: //The request is complete
document.getElementById(”content”).innerHTML = xmlHttp.responseText;
break
default:
document.getElementById(”content”).innerHTML = ‘Loading…’; //our loading text
}
}   function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject(”Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}

function Ajax(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
return true; //so follow the href url
} else {
var url=”index.php”;
url=url+”?page=”+str;
url=url+”&ajax=true”;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open(”GET”,url,true);
xmlHttp.send(null);
return false; //so do not follow the href url and stay on this page
}
}
</script>
</head>

<body>
<a href=”index.php?page=page1″ onclick=”return Ajax(’page1′)”>Load page 1</a><br>
<a href=”index.php?page=page2″ onclick=”return Ajax(’page2′)”>Load page 2</a><br>
<a href=”index.php?page=page3″ onclick=”return Ajax(’page3′)”>Load page 3</a><br>
<a href=”index.php?page=page4″ onclick=”return Ajax(’page4′)”>Load page 4</a><br>
<hr>
<div id=”content”>
<?php
include($_GET['page'] . “.txt”);
?>
</div>
</body>
</html>
<?php
}
} else {
header(’location: index.php?page=page1′);
}
?>

As you can see, it gets the content form the page-’id’ plus the .txt extension. Of course you can change this and get the content from a database or so, it is just an example.

Pakku

Share/Save/Bookmark

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in