Change pages url with ajax and load in another browser

5

I'm having the following problem, I'm developing a web application in java and would like to know how do I change the URL of a page when I click on a link in the menu. For example:

Access a website www.example.com.br this is the main link and there is another contact link, when I click on it, I would like to have www.example.com.br/contact. Also, I wanted to be able to copy this URL and open it in a different browser and go straight to the contact.

An example of this is Gmail, when we click to read an email the url changes and if we make a copy past in another browser it goes straight to the email.

Is it possible to do this as ajax? If not, which would be the best option?

    
asked by anonymous 29.12.2014 / 19:21

1 answer

3

As you mentioned that you are using jQuery, I recommend the History API plugin:

History

If you do not want to use a plugin, I recommend this article that details how to work with the API:

history.pushState and jQuery

The primary method of this API is history.pushState . It asks for 3 arguments, although the second is not used in some (all?) Browsers.

The first argument is a Javascript object associated with the new application state. This object is a parameter of the popstate event, triggered when a new state is navigated.

The second is the title of the new state.

The third is the URL of the new state. This URL can be relative or absolute and must be in the same domain. The browser will not run an HTTP call to this address, although in the case of a browser restart, it may be called, and in this case, your application should handle.

The popstate event, contrary to what the name suggests, is not triggered when a state is removed, but when the browser state changes. As stated, this event has a single parameter, which is the object associated with the state.

See more about this API in the Mozilla documentation: Manipulating the browser history .

As you are using jQuery, I recommend using a plugin instead of spending time reinventing the wheel.

    
29.12.2014 / 20:01