This is possible with JavaScript. You have two alternatives:
- go through the URL
- save to localStorage
Sample HTML
Assuming the content you want to pass is this:
<p id="comentario">Hoje está um lindo dia!</p>
Passing through the URL
You can enter this text in the URL that opens the next page. The format will be
http://teusite.com/pasta/subpasta?comentario=Hoje%20est%C3%A1%20um%20lindo%20dia!
The part of the URL after ?
is called querystring and exists to pass data between pages or server.
To insert content in the URL you will have to change the links in the HTML. The best way is with JavaScript using encodeURIComponent()
which is a native method to convert text to accepted format in the URL.
So you have to look for the link you want to open the next page and change:
Example:
HTML
<a id="ancora" href="novapagina.html">Clique aqui</a>
JavaScript
var texto = document.getElementById('comentario').innerHTML;
var ancora = document.getElementById('ancora');
ancora.href = ancora.href + '?comentario=' + encodeURIComponent(texto );
In this way, when the link is clicked it will open the new page with information in the URL / QueryString.
And how to read this on the new page?
To read this on the new page use location.search
to give you the querystring and then remove what you care about:
JavaScript
var qs = location.search;
var textoDesformatado = qs.split('=');
var textoFinal = textoDesformatado[1] && decodeURIComponent(textoDesformatado);
// e depois, para inserir no novo <p id="comentario"></p> basta fazer
document.getElementById('comentario').innerHTML = textoFinal;
note:
You can also open a new page with JavaScript directly. In that case it would look something like:
location.href = '/pasta/subpasta?comentario=' + encodeURIComponent(texto );
Passing through the localStorage
Local storage is the browser's memory and remains active even after you turn off the computer. That is the computer writes in an internal file of the browser and I saw later to look for this info.
So to write in the localStoragd just do:
localStorage.comentario = document.getElementById('comentario').innerHTML;
In the new page, to read the code, do the opposite:
document.getElementById('comentario').innerHTML = localStorage.comentario;
Note:
There are more alternatives using the server, but I do not include them because you just referred to client-side technology.