Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change URL in address bar without having to reload [duplicate]

Tags:

javascript

I've found a lot of questions about changing the url (without reloading). Some answers were - use plugins, use location.hash ..., or with reloading

But none of them worked for me.

On website I have a dropdown menu, and on its change the url parameter should have changed.

So what I'm trying to do is:

I want to change: www.foo.com?country=Germany into www.foo.com?country=Slovenia without reload.

Is what I am trying to achieve even possible?

like image 505
Sebastjan Avatar asked Mar 29 '13 20:03

Sebastjan


People also ask

How do I change URL but not reload?

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.

Which method is used to change the address in address bar of the browser?

HTML5 History API allows browsers to change the URL in browser address bar without reloading or refreshing the page using pushState function. The pushState method works similar to window.

How can we disable the address bar of browser so that no one can select the URL from the address bar?

16384.0 you can hide the address bar by setting location=no in the window. open features. Save this answer.


2 Answers

You can in newer browsers; in older ones, you can only change the hash. This seems like a good article on the topic: http://html5doctor.com/history-api/

like image 133
crimson_penguin Avatar answered Sep 27 '22 22:09

crimson_penguin


What you are looking for is the History API HTML5 provides. It comes with functionality like history.pushState(...), history.popState(...), which lets you dynamically change the URL without having to assign a new URL altogether.

It is used by many sites, including, I suspect, Facebook itself, where if you open a chat box, and navigate between pages, the chat box doesn't reload. It means that all new content are being fetched through Ajax, but then the URL won't change, would it? But it does. I think they do it through history.pushState(...), where you just push a new state into the History stack, and it changes only a certain part of the page. You will find an excellent tutorial here.

like image 29
SexyBeast Avatar answered Sep 27 '22 23:09

SexyBeast