Introducing
In order for the "Back" and "Forward" buttons that browsers provide to be used on a web-site whose contents are loaded asynchronously, it is necessary to manipulate the browser’s own browsing history.
The way in which this type of manipulation can actually be carried out without an update of the page (refresh), is through the use of hash locational.
Whenever we hash the current browser location, it will create a new entry in the browsing history. This will then allow the visitor to navigate a site whose contents are loaded asynchronously, through the use of the navigation arrows of the browser itself, without actually updating the page.
Needs
To enable the manipulation of hash of the current location in the browser, there are three basic requirements:
- Read: Collect and process the value of hash at current location;
- Write: Update to hash at the current location with the desired value;
- Listen: Detect when an amendment was made to hash and act accordingly.
Let’s see how to deal with each of these requirements:
Read
Collect the hash of current location
To read the hash of the current location, we can use Javascript and read the value of the property window.location.hash
. The first character will always be the indicator of hash #
, that can be disposed of.
// ler valor e "cortar" o primeiro caractere
var hashValue = window.location.hash.substring(1);
For the sake of efficiency, let us create a function to deal with this action:
/**
* Devolve a hash da localização actual
* @return {string} Valor da Hash com prefixo '#' ignorado.
*/
function lerHash () {
return window.location.hash.substring(1);
}
Write down
Write a value on hash of current location
To update the hash of the current browser location programmatically, we again resort to Javascript and the property window.location.hash
:
// atribuir valor à hash
window.location.hash = 'pagina2';
Once again, for reasons of efficiency, let us create a function to deal with this action:
/**
* Actualiza a hash da localização actual com o valor facultado
* @param {string} str
*/
function escreverHash(str) {
window.location.hash = str;
}
Listen to
Listen to and detect a change in hash current browser path
The browser conveniently triggers an event whenever the hash the current location has been updated or changed. To be aware of this update and trigger an action using the new value assigned, we just need to configure an event handler using Javascript to listen to this event:
/**
* Escuta alterações na hash da localização actual.
* @param {Event} e HashChangeEvent object
*/
window.onhashchange = function(e) {
// Fazer algo quando existe uma alteração de valor
// ...
};
Use of hash to allow a "persistent" status of the web page
To understand a little how everything is working behind the scenes, let’s look at the following scheme:
Action sequence A: Color Red
Here is illustrated the normal behavior of the browser, without the implementation of the requirements we have seen above:
- User interacts with page;
- The page updates the hash to open a new entry in browsing history;
- The update of hash triggers an event that in turn will call the manipulative function;
- Manipulative function does nothing.
Action sequence B: Color Green
Here is illustrated what happens with the application of the requirements we study above:
- User updates the address in the browser or use the "Back/Forward" buttons of the browser itself;
- The update of hash triggers an event that in turn will call the manipulative function;
- Manipulator function will perform a function our to update the page;
- Page is updated and nothing else happens (none refresh).
Practical example
Since in your question you have no code to better illustrate your practical scenario of adapting any answer to your specific case, here is a generic example.
In this example the web page simulates the insertion of contents via Ajax and manipulates the elements to be presented to the user, while trying to create new entries in the history of browser locations to allow the use of the "Back" or "Forward" buttons without causing an update (refresh):
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Exemplo de navegação sem actualização da página</title>
</head>
<body>
<nav>
<ul>
<li id="pagina1">
Página 01
</li>
<li id="pagina2">
Página 02
</li>
<li id="pagina3">
Página 03
</li>
<li id="pagina4">
Página 04
</li>
</ul>
</nav>
<section>
Olá, bem vindo à Página 01!
</section>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function cumprimenta(pagina){
$('section').delay(100).html( "Olá, bem vindo à " + $('#'+pagina).html() );
$('nav li').css("color", "black");
$this.css("color", "green");
}
function oraBolas() {
$('section').delay(100).html( "Existe qualquer coisa de errado!" );
}
/**
* Escuta alterações na hash da localização actual.
* @param {Event} e HashChangeEvent object
*/
window.onhashchange = function(e) {
var pagina = lerHash();
// aqui chama-se função X mediante o valor da variável "pagina"
if (pagina.length==7)
cumprimenta(pagina);
else
oraBolas();
};
/**
* Actualiza a hash da localização actual com o valor facultado
* @param {string} str
*/
function escreverHash(str) {
window.location.hash = str;
}
/**
* Devolve a hash da localização actual
* @return {string} Valor da Hash com prefixo '#' ignorado.
*/
function lerHash () {
return window.location.hash.substring(1);
}
$(function() {
$('nav').on("click", 'li', function (e) {
e.preventDefault();
escreverHash($(this).attr("id"));
});
});
</script>
</body>
</html>
Completion
In this way, manipulating the hash, a consistent navigation throughout the web-site is achieved, either by the use of the links present in the page currently being presented, or by the use of the arrows "Back" and "Forward" of the browser itself.
The work is all performed by the listening event of the hash, where the relevant function which will give rise to the intended behaviour or changes is to be called here.
I think you can solve both problems in the question like this:
- Direct the visitor to a certain area without refresh;
- Ensure that, if the visitor uses the navigation arrows present in the browser itself, the web-site will present you the page previous or next as expected once again without refresh.
I spent two hours preparing an answer, but now your question indicates a detail that makes it impossible in a way!!! I will leave it anyway because it deals with most of the problems presented. It just does not solve the question of using something other than the hash to control the URL.
– Zuul