Forum Moderators: open

Message Too Old, No Replies

Making AJAX crawlable

making ajax crawlable

         

emshabir

11:21 am on Nov 9, 2011 (gmt 0)

10+ Year Member



Hello all,

I have a page that displays a random row from a mysql db using ajax whenever this button is clicked:
<a href="#" onclick="ajax_request()">

I have read a few articles on how google recommends using ajax so that crawler can access the content. but I can't tell if the specific job that my ajax is doing is anyway crawlable or not.

Is there hope? Is there any way that reading a random row from a DB can be programmed to be google friendly?

I appreciate any help or hints you can give me on the subject.

rocknbil

5:12 pm on Nov 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard emshabir. This is so easy you're going to laugh. :-)

"ajax_request()" queries some script or something, right? let's say it just requests random.php and random.php outputs your row. All you'll need to do is add return false to the end of ajax_request();

function ajax_request() {
// your code
return false;
}

With that in place, you change your link like so:

<a href="random.php" onclick="return ajax_request()">

By returning false on click, it stops the browser from executing the navigation. More important than "crawlable", if Javascript is disabled for your users, they can still access your content.

I'd add a little bit though. Now that the link will be followed, all it will display is your single line. For the link itself, I'd pass a variable* :

<a href="random.php?full_page=1" onclick="return ajax_request()">

So in random.php, you would do

if (isset($_GET['full_page']) and ($_GET['full_page']==1)) {
// output a full (X)HTML page, with the same body content
}
else {
// output the one random line for ajax
}

The concepts are the same for perl, ASP, CF . . .

The "same content" is important, if you fill up the page with something different than what users see, it may be considered gray/black S.E. gaming, which will get the page dropped in the index (and takes forever to recover . . .)

* Advanced: in terms of search engines, I really wouldn't use a query string. The whole point is making your URL's and content relevant, so I'd want a relevant URL there. I'd probably do a rewrite, so my link would be something like

<a href="/random-quote-of-the-day" onclick="return ajax_request()">

then in .htaccess/mod_rewrite (or Windows rewrites, whatever)

ReWriteRule ^random-quote-of-the-day/?$ /random.php?full_page=1 [NC,L]

emshabir

3:13 pm on Nov 12, 2011 (gmt 0)

10+ Year Member



Rocknbil! You sir are a god-sent. I will name my first born after you. :)

There is something I don't quite understand though. Why would I pass a variable? What function could a variable play?

are you saying that "random.php?full_page=1" should correspond with the first row on the DB? "random.php?full_page=2" should correspond with the second row?

thanks!

rocknbil

5:20 pm on Nov 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why would I pass a variable?


Now that the link will be followed, all it will display is your single line.


Your "randomizer" just outputs a single line (or, a small chunk of data) without HTML headrs, footers, navigation, or formatting, right? Now that SE's can follow your link, OR if the user is browsing with Javascript disabled, that's all they will see.

So passing the variable in the link (or using the mod_rewrite trick below) will "tell" your script to present this data as a full page, with navigation, etc. When called form the Ajax, it won't pass the variable, so it will just output the single line/data chunk, just like it does now.