Sep 4 2008

jQuery and AJAX Requests

Ive written ajax request the long handed way using the long hand way of a XMLHttpRequest. It can be tedious and frustrating. Don’t get me wrong, it works great but building this functionality can take a long time, just to avoid a page refresh.

This is where jQuery comes in handy. In a few lines of javascript you can create quick ajax requests. This is how its done:

1. Download the jQuery library and include this file in your head tag like so.

<script type=”text/javascript” src=”path-to-file/jquery.js”></script>

2. Create a php file that will be called and return value to the DOM. This can be a simple file that echos ‘Hello World’.

3. Create a page element that receives the markup. Here would be an example:

<div id=”loadedContent”></div>

4. Now that you have a php file that returns content, and a page element that can recieve the content you can now create the javascript/jQuery to make the ajax request and input the results into the DOM.

<script type=”text/javascript”>
$(document).ready(function () {
$.post(”hello-world.php”, function(data){
$(”#loadedContent”).html(data);
});
});
</script>

That’s it! On the page load it should request the page. You can go further by using simple click events and send simple name/value pairs using the same request. jQuery is simple and easy to use. If you have any questions please leave a comment.


May 17 2007

Too Much AJAX, is it good for SEO?

Well I did it, just upgraded to Adobe Web Premium Edition. First I would like to say I love it. It is actually based on web standards and not IE6 which I am grateful for. But one thing I feel got “too easy”, that’s AJAX. I like the way Dreamweaver using Spry to build AJAX into your site, but there is one problem, one word JavaScript. Search Engines have been known to have problems with links and content generated by JavaScript. Don’t get me wrong, I love JavaScript, it’s a great client side scripting language, but with Spry I really feel it will be overused.

Now that it is easy to build these “widgets” what stops someone from building all their content onto one page fed by an xml file? Not much except ignorance by which search engines will index the content. I feel a lot of web designers clients are going to get hosed with regards to engine traffic once a very creative and non-technical designer get a hold of this tool. That’s ok, if everyone starts using this it will just get easier for others that understand SEO to rank well. This is like the Flash epidemic.