Exchange of content between pages with Javascript

Asked

Viewed 2,247 times

2

I need to exchange a text that is in a <p> from one page x to one page <p> one-page y.

As part of a bimonthly presentation (course work) I can use HTML, CSS3 and Javascript.

You can do that with some of these technologies?


Excuse my lack of clarity with the question, I wanted to actually leave most of the texts saved on a page that would not be loaded and would serve only for such, being accessed only the text in question and displayed on the page where the user was. I researched a little on the subject and could only do something like this by php. Sorry for the work and thanks.

  • 3

    You can better explain how you navigate between pages, if they are in the same domain?

  • Have you done anything? Show some code.

  • Do you want to take a paragraph from one HTML page to another HTML page? For this you can use Javascript, with different ID in HTML.. But I believe that you are a little confused or explained wrong.

  • css3 is technology to style pages and not to "load pages". I think you need to first understand what each technology does.

2 answers

3

This is possible with Javascript. You have two alternatives:

  • go through the URL
  • store in the localStorage

HTML example

Assuming the content you want to pass is this:

<p id="comentario">Hoje está um lindo dia!</p>

Going through the URL

You can insert this text into the URL that opens the next tab. 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 into the URL you will need to change the links in the HTML. The best way is with Javascript using encodeURIComponent() which is a native method for converting text into accepted format in the URL.

So you have to find 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 );

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 uses the location.search to give you the querystring and then remove what interests you:

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 Javascript page directly. In this case it would be something like:

location.href = '/pasta/subpasta?comentario=' + encodeURIComponent(texto );

Going by the localStorage

The Storage location is the browser memory and is active even after shutting down the computer. In other words, the computer writes to an internal file in the browser and I saw it later.

So to write to localStoragd just do:

localStorage.comentario = document.getElementById('comentario').innerHTML;

and, on the new page, to read the code just do the opposite:

document.getElementById('comentario').innerHTML = localStorage.comentario;

Note:

There are more alternatives using the server, but I don’t include them because you only mentioned client-side technology.

  • The answer is good, but I’d have to make a lot of extra pages, and that’s something I want to avoid. For example: The site I’m making is about mythologies, has the site and an image with each god, when clicking on the image it appears in a < aside > and the text appears next to the deity. I would be obliged to create a page for each god if I made this system. And it will be 3 different pantheons and all long.

0

has how to do this using various technologies and in various ways. but using only html/css, here goes:

pagina1.html

<h1>origem</h1>
<p id="p1">Primeiro</p>
<a href="pagina2.html" id="link">ir para a página 02</a>
<script>
    $('#link').attr( 'href', 'pagina2.html?' + $('p1').html() );
</script>

page2.html

<h1>destino</h1>
<p id="p2"></p>
<script>
    var content = (location+"").split('?')[1].split('=')[1];
    $('#p2').html(content);
</script>
  • 1

    "Only HTML/CSS" You used only one HTML file to do this, but you used Javascript and HTML. There’s no way to do this without Javascript. But as the author was not clear with his question, it may have helped.

  • Hi Fabio, welcome to [en.so]! In your code I don’t see it being necessary .split('=')[1];. Either you can take that part out or you can add the key to the querystring: ?p=. It’s good to remind the author of the question (AP) that you are using jQuery, which has to be added to the page

Browser other questions tagged

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