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

I have a link on my webpage, say "o." Clicking on it loads a specific div without refreshing the entire page using jquery .load() . This does not change the URL bar in the address bar of the browser.

You can go to the same page by selecting www.mydomain.com/?page=about .

So what I want to do when the user clicks the "about" link, the pages will load as they are (using jquery), but I want to change the URL bar in the address bar of the browser so that someone can actually copy or tag exact page.

Can this be done?

+6
source share
2 answers

You have two options to solve this problem:

  • In new browsers, you can use the HTML5 History API , which allows you to change part of the URL, as well as the query string (and afaik path),

  • In browsers that do not support this, you can only change the identifier of the fragment # without reloading the page (via location ). That means you need to change the url for example.

     www.mydomain.com/#!page=about 

    #! is an agreement from Google to make Ajax sites crawlable . Since the fragment identifier is not sent to the server, you must detect it using JavaScript and download the corresponding data from the server.
    There are jQuery plugins to help you handle this.

I would look for a good plugin using the history API, if available, and return to the hash solution.


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

+5
source

Yes, I did it by doing

 location.hash = 'foo'; 

There are other location attributes that you can change, but are not sure if it caused ?? probably a query string, get, or something like that.

-1
source

Source: https://habr.com/ru/post/887348/


All Articles