Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the URL string in the address bar using javascript

I've a link on my webpage, say 'about'. Clicking on it loads a particular div without refreshing the whole page using jquery .load(). This does not change the URL string in the browser address bar.

The same page can be accessed by going to www.mydomain.com/?page=about.

So what I want to do is, when the user clicks on the 'about' link, the pages will be loaded as it is (using jquery), but I want to change the URL string in the browser address bar also so that someone can actually copy or bookmark the exact page.

Is it possible to do so??

like image 908
ptamzz Avatar asked May 05 '11 09:05

ptamzz


People also ask

How do I change URL without reloading?

There is no way to modify the URL in the browser without reloading the page. The URL represents what the last loaded page was. If you change it ( document. location ) then it will reload the page.

Is URL the same as address bar?

The address bar is where you enter a website's URL (Uniform Resource Locator) or address. It is located usually at the very top of a browser window.

Does the address bar contains the URL?

The address bar is the familiar text field at the top of a web browser's graphical user interface (GUI) that displays the name or the URL (uniform resource locator) of the current web page. Users request websites and pages by typing either the name or the URL into the address bar.


1 Answers

You have two possibilites to tackle this problem:

  • In newer browsers you can make use of the HTML5 history API, which lets change part of the URL, also the query string (and path afaik).

  • In browsers which don't support this, you can only change the fragment identifier # without reloading the page (via the location object). That means you have to change the URL to e.g.

    www.mydomain.com/#!page=about
    

    #! is a convention from Google to make Ajax sites crawlable. As the fragment identifier is not sent to the server, you have to detect it with JavaScript and load the corresponding data from the server.
    There are jQuery plugins that help you to handler this.

I would look for a good plugin makes use of the history API if available and falls back to the hash based solution.


As written in my comment, you can also find more information here:

  • How to change browser address bar without reloading page, especially @ThiefMaster's answer.
like image 172
Felix Kling Avatar answered Oct 10 '22 01:10

Felix Kling