Our first order of business is to get those back and forward buttons working, and allow our site visitors to bookmark and share links to individual pages. That's why we've included the jQuery BBQ plugin.
scripts.js
and replace it with a simple document ready statement as follows:$(document).ready(function(){ // Our deluxe ajaxy code goes here });
$(document).ready(function(){
$('#ajax-nav a').each(function(){
$(this).attr('href', '#' + $(this).attr('href'));
});
});
We're selecting all the links in the main navigation, then looping through all of them to add a # character at the front of the URL. For example, the cupcakes.html
link is now #cupcakes.html
. If you refresh the page in the browser, you'll see that clicking the links doesn't change anything on the page, but it does update the hash sin the URL in the browser's location bar.
hashchange
event. Modern browsers have provided an event called hashchange
that fires whenever the URL's hash changes, just as it's doing when you click the main navigation links. Older browsers don't support the hashchange
event, but that's where the jQuery BBQ plugin comes in. It provides support for a pseudo hashchange
event in most browsers so that we only have to write our code once without worrying about browser differences. Here's how we bind a function to the hashchange
event:$(document).ready(function(){
$('#ajax-nav a').each(function(){
$(this).attr('href', '#' + $(this).attr('href'));
});
$(window).bind('hashchange', function(e) {
// our function goes here
});
});
$(document).ready(function(){
$('#ajax-nav a').each(function(){
$(this).attr('href', '#' + $(this).attr('href'));
});
$(window).bind('hashchange', function(e) {
var url = e.fragment;
$('#main-col').load(url + ' #main-col-wrap'),
});
});
First, we're setting up a variable called url
and setting it equal to e.fragment.
The fragment
property is made available by the jQuery BBQ plugin. It's equal to
the hash of the URL without the hash symbol. So if the window's hash changes to
#cupcakes.html, e.fragment
will be equal to cupcakes.html.
The next line of code is the same as our basic Ajax navigation example. I'm going
to select the container on the page where I want to load my content, then call the
load()
method. I'm going to pass the URL and jQuery selector for the part of the
page at that URL that I want to load into the browser.
If you refresh the page in the browser now, you'll see that our main navigation is again working asynchronously. Clicking a link loads up only the main content area of the page while the rest remains unchanged. There is one important difference, though—if you click the back and forward buttons, they work. Once you've clicked through to the cupcakes page, you can click the back button to return to the home page.
#cupcakes.html
hash is there, we just have to tell our code to look for it. The simplest way to do that is to fire the window's hashchange
event as soon as the page loads in the browser. Here's how we do that:$(document).ready(function(){
$('#ajax-nav a').each(function(){
$(this).attr('href', '#' + $(this).attr('href'));
});
$(window).bind('hashchange', function(e) {
var url = e.fragment;
$('#main-col').load(url + ' #main-col-wrap'),
});
$(window).trigger('hashchange'),
});
Now, you can open up that cupcakes link in a new window and you'll see the cupcakes page load up, just as it should. Our hashchange
function fires as soon as the page is loaded, which loads in the correct content.
What just happened?
We used jQuery to loop through each of our navigation links and replace them with internal links or hash links. Why not just do this in HTML? Because we want to make sure that our page continues to work for users with JavaScript disabled.
Then we used the jQuery BBQ plugin to change our asynchronous navigation to enable both bookmarking and sharing of links and the back and forward buttons in the browser. This allows our site to behave just like a single-page application without breaking the site visitor's expected experience.
3.15.143.181