views:

130

answers:

1

I haven't found a good resource in using # in urls to enable bookmarking with ajax calls so I have a few questions.

We have a site based on CakePHP using 'pretty' urls essentially www.mysite.com/controller/action/param1:param2 I noticed that Facebook uses a weird syntax with their pretty urls ex) #!/?ref=logo

Is there a way we can use # marks to enable bookmarking this way keeping our 'pretty' urls?

Thanks

A: 

Only if you use Javascript to redirect to the correct URL.

PHP, and any other server-based platform does not have access to the data after the hash mark, and therefore is only for use in Javascript and other client-side languages.

You could put some Javascript code to correctly load a page via AJAX based on the tag. However, it is probably not of much value unless your website is a complete application. AKA, most of the stuff that happens doesn't require a refresh.

An example of this would be (in jQuery):

$('a').click(function(){
    var page_url = $(this).attr('src');

    // Set the hash and load the page into the main container
    location.hash = page_url;
    someLibrary.load(page_url);
});
Chacha102
Is there some way to change the url adding /param/ or can we only add things after the hash?
Sorry should have mentioned I use YUI
You can 'redirect' the page by changing `location.href` I believe. I think that specifically doesn't contain the hash value. Although I'm not certain about that.
Chacha102
Ok so essentially to do this we would add a value after the #... say /action#123456 then when the page loads, before displaying anything, check if the # exists, if it does, then pass that parameter to the server to load the actual page? Seams kind of bulky
That is why you don't see many non-application based websites use it, and for at least Facebook, only a part of the content is based on it. PHP sends the menu bars and all that, but the live feeds and the main content of the page is loaded by Javascript.
Chacha102
This is embarrassing but YUI3 has a 'History' module for just this reason. http://developer.yahoo.com/yui/3/history/
You'll notice the loading screen everytime you log into Gmail. That is Gmail's Javascript engine warming up, checking if you are going to a bookmarked page, etc.
Chacha102
@razass - It is. The primary use is to keep certain elements persistent in the page (eg. facebook chat) while still loading different content based on the hash
K Prime