views:

83

answers:

4

I use 'window.location.hash' to add '#something' to the URL without refreshing the page.

I want to know how to do the same but using a slash (/) instead of hash (#).

Why? I have navigation tabs and I use a jQuery and Ajax to dynamically load the data. When javascript is enabled, '#something' is added to the end of the URL to get the data. When javascript is disabled, it redirects to '/something'. So I want to fake the same URL for both.

Instead of http://site.com/section#something -> http://site.com/section/something

Thanks.

A: 

Why don't you deploy SWFAddress?

It does get you URLs in the form of ../#/section/something and should be pretty much what you need. It's widely used for many Flash/AJAX websites on SEO considerations.

Ain
+2  A: 

You can use any combination after the hash you want, but the answer to your question is no, you can't do what you're asking without re-directing the user.

Being able to play with the URL without re-directing would be a security concern on some levels (can you change the domain too? why not?....see where this rabbit hole goes?). For example changing your URL via JavaScript to say: http://www.mybank.com (why isn't my bank using SSL? bad bank, bad!) would be a phishers dream...so browsers don't allow messing with the URL like this at all...not without actually taking you there.

Nick Craver
+2  A: 

Take a look at this article. Basically, it lets you do this:

history.pushState({}, 'New Title', 'new_page.html');

This updates the history and the location bar but not actually load the page. That's what you want, but it's part of HTML5, and few (if any) browsers support it at the moment. Sticking with hashes is a better idea.

Casey Hope
+1 that's nice to know.
Anurag
+1  A: 

History.pushState (see the link in @Casey's post, or Kyle Scholz' blog) is present in the latest versions of Safari and Firefox, and Modernizr 1.5 now tests browser support for it. I just starting playing around with this today and it appears to do exactly what you want.

I realize this doesn't help with older browsers; some kind of window.location.hash trick will still be needed there.

Robert Calhoun