tags:

views:

25257

answers:

9

How would I have a JavaScript action that may have some effects on the current page but would also change the URL in the browser so if the user hits reload or bookmark the new URL is used? It would also be nice if the back button would reload the original URL.

EDIT: As moonshadow correctly assumes I am trying to record JavaScript state in the URL.

+8  A: 

I would strongly suspect this is not possible, because it would be an incredible security problem if it were. For example, I could make a page which looked like a bank login page, and make the the URL in the address bar look just like the real bank!

Perhaps if you explain why you want to do this, folks might be able to suggest alternative approaches...

Paul Dixon
Not so much of an issue if non-reload changes were restricted to the path, query string, and fragment—i.e. not the authority (domain and such).
Ollie Saunders
http://www.youtube.com/user/SilkyDKwan#p/u/2/gTfqaGYVfMg is a example that it is possible, try switching videos :)
Spidfire
You can only change the location.hash (everything after the #), as Matthew Chumley notes in the accepted answer.
Paul Dixon
+35  A: 

Set the window.location.hash property to some value that ideally has some kind of meaning. Be careful of colliding with ids on the page though, because the browser will scroll to any element with a matching id.

Matthew Crumley
Don't search engines ignore these internal links (hashes)? In my scenario I want spiders to pick up on these links too.
Drew Noakes
@Drew, as far as I know, there's no way for search engines to treat a part of a page as a separate result.
Matthew Crumley
There is now a proposal to make search engines see hashes: http://googlewebmastercentral.blogspot.com/2009/10/proposal-for-making-ajax-crawlable.html
LKM
+5  A: 

Browser security settings prevent people from modifying the displayed url directly. You could imagine the phishing vulnerabilities that would cause.

Only reliable way to change the url without changing pages is to use an internal link or hash. e.g.: http://site.com/page.html becomes http://site.com/page.html#item1 . This technique is often used in hijax(AJAX + preserve history).

When doing this I'll often just use links for the actions with the hash as the href, then add click events with jquery that use the requested hash to determine and delegate the action.

I hope that sets you on the right path.

Jethro Larson
+16  A: 

window.location.href contains the current URL. You can read from it, you can append to it, and you can replace it, which may cause a page reload.

If, as it sounds like, you want to record javascript state in the URL so it can be bookmarked, without reloading the page, append it to the current URL after a # and have a piece of javascript triggered by the onload event parse the current URL to see if it contains saved state.

If you use a ? instead of a #, you will force a reload of the page, but since you will parse the saved state on load this may not actually be a problem; and this will make the forward and back buttons work correctly as well.

moonshadow
recording javascript state in the URL is exactly what I'm trying to do
Steven Noble
+1  A: 

There is a Yahoo YUI component (Browser History Manager) which can handle this: http://developer.yahoo.com/yui/history/

powtac
+1  A: 

I was wondering if it will posible as long as the parent path in the page is same, only something new is appended to it.

So like let's say the user is at the page: http://domain.com/site/page.html Then the browser can let me do location.append = new.html and the page becomes: http://domain.com/site/page.htmlnew.html and the browser does not change it.

Or just allow the person to change get parameter, so let's location.get = me=1&page=1.

So original page becomes http://domain.com/site/page.html?me=1&page=1 and it does not refresh.

The problem with # is that the data is not cached (at least I don't think so) when hash is changed. So it is like each time a new page is being loaded, whereas back- and forward buttons in a non-Ajax page are able to cache data and do not spend time on re-loading the data.

From what I saw, the Yahoo history thing already loads all of the data at once. It does not seem to be doing any Ajax requests. So when a div is used to handle different method overtime, that data is not stored for each history state.

A: 

window.location('address_goes_here');

This causes a new request.
harpo
A: 

lol it wouldn't ve risk, if you couldn't change domain...

dado
A: 

If this isn't possible, how do Facebook do it? Check out when you press 'Next' and 'Previous' on the photo gallery page...

Adam