views:

289

answers:

1

Hello,

I have a website which has many pages:

For example:

HOME: http://mywebsite.com/index.html

SOME PAGE: http://mywebsite.com/categorie/somepage.html

I decided to make my pages load dynamically with AJAX without reloading the page. So I decided to use jQuery Address plugin ( http://www.asual.com/jquery/address/docs/ ) in order to allow deeplinking and Back-Forward navigation:

<script type="text/javascript" src="uploads/scripts/jquery.address-1.2rc.min.js"></script>
<script type="text/javascript">
                $('a').address(function() {
                    return $(this).attr('href').replace(/^#/, '');
                });
</script>

Now, after installing the plugin, if I go on http://mywebsite.com/index.html (HOME) and click on SOME PAGE link, jquery successfully loads the http://mywebsite.com/categorie/somepage.html without reloading the page and the address bar on my browser displays: http://mywebsite.com/index.html/#/categorie/somepage.html which is great!

However, the problem is: if I copy this dynamically generated URL: http://mywebsite.com/index.html/#/categorie/somepage.html into a web browser address bar, it will take into my website index.html page and not to the "SOME PAGE" page. Also, The Forward/Back buttons don't work correctly, they only replace the address in the URL bar but the content stays the same.

I suppose that I need to write some JavaScript rule that associates the dynamic URL with the correct page?

Some help would be appreciated. Thanks :)

A: 

Something like this:

$(function () {
    var path = location.hash.substring(1); // remove '#'
    if (path) {
        $.address.value(path);
    }
});

Update:

If you're loading pages manually (e.g. on link click) instead of using an address event handler (e.g. $.address.change(function () { ... })), then replace the $.address.value(path); above with an Ajax call for the page at path:

$(function () {
    var path = location.hash.substring(1); // remove '#'
    if (path) {
        // get page at path
    }
});
Jeffery To
that doesn't seem to work... :( thanks for your help anyway
antosha
I've updated the answer
Jeffery To
I am sorry, but I don't really understand.However, I found another plugin, which seems to offer the same thing:http://benalman.com/projects/jquery-bbq-plugin/ this one works using a via hashchange event. I'm going to give it a try.
antosha