Change url of pages with ajax and click on another browser

Asked

Viewed 886 times

5

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

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

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

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

  • This is actually a mix of History API and routing. I don’t know if it’s possible to do this in Java. Or are you referring to Javascript?

  • In javascript if possible. I can load the content I want into a div, but I wanted to do this url scheme as described above or else what is the best way to do it ?

  • you are using Java or Javascript?

  • In this case, update your question. Java and Javascript are two completely different things, despite the name.

  • I am using javascript

1 answer

3

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

History

If you don’t want to use a plugin, I recommend this article that deals in detail how to work with the API:

history.pushState and jQuery

The main method of this API is the 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 state of the application. This object is the event parameter popstate, fired 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 browser reboot, it can be called, and in this case, your application should treat.

The event popstate, Contrary to what the name suggests, it is not triggered when a state is removed, but when the state of the browser is changed. 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 this using jQuery, recommend using a plugin, rather than spending time reinventing the wheel.

  • History API, with this api I can copy the url and pass for example to someone else access, IE, following the same principle of gmail ?

  • Yes, it provides the necessary resources for this. Gmail itself uses it (it is the API for this).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.