1

I'm wondering if there is a way to change the URL that is displayed in a browser that does not support HTML5? I update some pages using AJAX, so it would be useful to be able to do this to allow users copy and paste links.

Thanks.

David Gard
  • 11,225
  • 36
  • 115
  • 227
  • i find it hard to understand exactly what you mean `change the URL that is displayed in a browse` and `allow users copy and paste links` – david Oct 26 '11 at 09:43
  • @david At the top of any page the URL is displayed. I am updating page content using AJAX, but doing this doesn't update the URL that is shown to the user. If a user called Geoff likes the contect and thinks "I'll copy that link and share it with Bob", then he will be copying the wrong URL, directing Bob to the origianal page that Geoff viewed. – David Gard Oct 26 '11 at 09:50

1 Answers1

2

Usually, website programmers that want to change the site URL when using AJAX, add a hash to the URL (#) and then a page component specific URL.

To change the hash itself use window.location.hash.

However, since the browser does not send the hash portion of the URL to the server, you'll need some code to send that part explicitly to the server to fetch the right content.

There are several jQuery plugins that deal with this issue, for example http://plugins.jquery.com/plugin-tags/hash, and there are many questions here that deal with the subject:

Getting URL hash location, and using it in jQuery

Encoding of window.location.hash

Parsing URL hash/fragment identifier with JavaScript

These are just a few examples.

Community
  • 1
  • 1
Kraylog
  • 7,383
  • 1
  • 24
  • 35
  • Ok, so basically all I could do with the older browsers is - http://mydomain.com/#page_name, which when navigated to would cause my site to say "hang on, there is a # there - thats load the content for http://mydomain.com/page_name"? – David Gard Oct 26 '11 at 10:00
  • Actually, that's true for the newer browsers as well. The main difference is, that in older browsers you didn't have an event if the hash value changed. So, if the user navigated (say, pressed the back button) within your site, a new request wouldn't get sent. – Kraylog Oct 26 '11 at 11:59