views:

117

answers:

6

I build a website focussing on loading only data that has to be loaded. I've build an example here and would like to know if this is a good way to build a wegpage. There are some problems when building a site like that, e.g.

  • bookmarking
  • going back and forth in
  • history SEO (since the content is basically not really connected)

so here is the example:

index.html

<html>
<head>
<title>Somebodys Website</title>
  <!-- JavaScript -->
  <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  <script type="text/javascript" src="pagecode.js"></script>
</head>
<body>
<div id="navigation">
<ul>
 <li><a href="#" class="nav" id="link_Welcome">Welcome</a></li>
 <li><a href="#" class="nav" id="link_Page1">Page1</a></li>
</ul>
</div>
<div id="content">
</div>
</body>
</html>

pagecode.js

var http = null;
$(document).ready(function()
{
// create XMLHttpRequest
try {
 http = new XMLHttpRequest();
}
catch(e){
 try{
  http = new ActiveXObject("MS2XML.XMLHTTP");
 }
 catch(e){
  http = new ActiveXObject("Microsoft.XMLHTTP");
 }
}
// set navigation click events
$('.nav').click(function(e)
  {
 loadPage(e);
  });
});

function loadPage(e){
  // e.g. "link_Welcome" becomes "Welcome.html"
  var page = e.currentTarget.id.slice(5) + ".html";

  http.open("POST", page);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Connection", "close");
  http.onreadystatechange = function(){changeContent(e);};
  http.send(null);
}

function changeContent(e){
  if(http.readyState == 4){
 // load page
 var response = http.responseText;
 $('#content')[0].innerHTML = response;
  }
}

Welcome.html

<b>Welcome</b>
<br />
To this website....

So as you can see, I'm loading the content based on the IDs of the links in the navigation section. So to make the "Page1" navigation item linkable, i would have to create a "Page1.html" file with some content in it.

Is this way of loading data for your web page very wrong? and if so, what is a better way to do it?

thanks for your time

EDIT:

this was just a very short example and i'd like to say that for users with javascript disabled it is still possible to provide the whole page (additionally) in static form. e.g.

<li><a href="static/Welcome.html" class="nav" id="link_Welcome">Welcome</a></li>

and this Welcome.html would contain all the overhead of the basic index.html file. By doing so, the ajax using version of the page would be some kind of extra feature, wouldn't it?

A: 

How about unobtrusivity (or how should I call it?)?

If the user has no javascript for some reason, he'll only see a list with Welcome and Page1.

Natrium
+5  A: 

It's wrong to use AJAX (or any javascript for that matter) only to use it (unless you're learning how to use ajax which is diffrent matter).

There are situations where the use of javascript is good (mostly when you're building a custom user interface inside your browser window) and when AJAX really shines. But loading static web pages with javascript is very wrong: first, you tie yourself with a browser that can run your JS, second you increase the load on your server and on the client side.

kender
The User Interface point is a really good one. I still think there are ways to unobtrusively implement it on other frontends though.
Daff
+5  A: 

No, it isn't a good way to do it.

Ajax is a tool best used with a light touch.

Reinventing frames using it simply recreates all the problems of frames except that it replaces the issue of orphan pages with complete invisibility to search engines (and other use agents that don't support JS or have it disabled).

By doing so, the ajax using version of the page would be some kind of extra feature, wouldn't it?

No. Users won't notice, and you break bookmarking, linksharing, etc.

David Dorward
"No it isn't?" What question are you answering?
Dominic Rodger
Umm. The one in the title. I thought that was obvious.
David Dorward
Ah, my apologies. The OP has two questions - 1. "Good way to do it?" (in the title); 2. "Is this way of loading data for your web page very wrong?" (in the body). I missed the former.
Dominic Rodger
I missed the latter :) Edited for clarity.
David Dorward
+1 for the SEO reference, your page will not be indexed this way.
Colin
A: 

Yes it's wrong. What about users without JavaScript? Why not do this sort of work server-side? Why pay the cost of multiple HTTP requests instead of including the files server-side so they can be downloaded in a single fetch? Why pay the cost of non-JavaScript enabled clients not being able to view your stuff (Google's spider being an important user who'll be alienated by this approach)? Why? Why?

Dominic Rodger
Well, the basic idea behind this was to load only whats necessary.I knew that this would bring up problems, but for most of them there is a workaround.users without js - provide a static backup version of the page(solves also the search engine part)
gabtub
+1  A: 

More technical details:

The function loadPage should be re-written using jquery : $post(). This is a random shot, not tested:

function loadPage(e){
  // e.g. "link_Welcome" becomes "Welcome.html"

  var page = e.currentTarget.id.slice(5) + ".html";
  $.post( page, null, function(response){
      $('#content')[0].innerHTML = response;
  } );
}

Be warned, I did not test it, and I might get this function a little wrong. But... dud, you are using jQuery already - now abuse it! :)

elcuco
thanks for pointing that out :)
gabtub
BTW, why post? what's wrong about a simple "get"?
elcuco
For completeness take a look at http://docs.jquery.com/Ajax
Samuel
+1  A: 

When considering implementing an AJAX pattern on a website you should first ask yourself the question: why? There are several good reasons to implement AJAX but also several bad reasons depending on what you're trying to achieve.

For example, if your website is like Facebook, where you want to offer end-users with a rich user interface where you can immediately see responses from friends in chat, notifications when users post something to your wall or tag you in a photo, without having to refresh the entire page, AJAX is GREAT!

However, if you are creating a website where the main content area changes for each of the top-level menu items using AJAX, this is a bad idea for several reasons: First, and what I consider to be very important, SEO (Search Engine Optimization) is NOT optimized. Search engine crawlers do not follow AJAX requests unless they are loaded via the onclick event of an anchor tag. Ultimately, in this example, you are not getting the value out of the rich experience, and you are losing a lot of potential viewers.

Second, users will have trouble bookmarking pages unless you implement a smart way to parse URLs to map to AJAX calls.

Third, users will have problems properly navigating using the back and forward buttons if you have not implemented a custom client-side mechanism to manage history.

Lastly, each browser interprets JavaScript differently, and with the more JavaScript you write, the more potential there is for losing cross browser compatibility unless you implement a framework that such as jQuery, Dojo, EXT, or MooTools that handles most of that for you.

pixelbobby
I'm a firm believer in creating sites that can be read by the average user, the blind, the deaf, the fingerless, and more importantly, **robots**. AJAX is MAINLY used to enhance the user experience, decrease page load times, etc. Any of your links that may be loading DATA or CONTENT should also have static versions that search engines can read. IE, if your "next" link on a page of products loads the content dynamically... well, when a user using a not so javascript compliant browser (read: robots) hits your page, they should be getting a static link to load that data, not an AJAXy request.
snicker
Nice write-up. Thanks for sharing, snicker.
pixelbobby