0

For example the webpage : www.test.com/a123 has a var called Value that stores quantity of products the webpage www.test.com/b123 has the same variable also.

On the console of each page, I can call the variable and get their values, but there is some way to get this values without being on that specific page?

I need to get the value table from this pages having only to specify which page I would like.

Something like :

document.getElementByClassName('tamanho__item_pdpjs-tamanho__item_pdp')[5]

But for variables referencing from which page to get

Mickael B.
  • 4,755
  • 4
  • 24
  • 48
Camlost
  • 11
  • 1

2 Answers2

2

Use the localStorage property of JS.

 localStorage.setItem("a123", "value");

  // Retrieve on b123 page
  document.getElementById("result").innerHTML = localStorage.getItem("a123");
  • When I use this approach, it returns a – Camlost May 03 '20 at 03:34
  • Provide js fiddle link. I'll do the changes. –  May 03 '20 at 03:36
  • I think I don't know how to do this, but I try to explain deeper. Nike.com has a webpage for each sneaker they have, in each sneaker page, there is the same variable that I will call "SKUsSizes". When I type in console SKusSizes[40] console returns a list of details like Id,size code,price. But to get this values I have to be on that specifc sneaker webpage. – Camlost May 03 '20 at 03:44
  • I understand. Does this help you? https://stackoverflow.com/questions/36146595/can-we-refer-to-javascript-variables-across-webpages-in-a-browser-session –  May 03 '20 at 03:48
  • I will look deeper into it as I think the problem is similar, Thanks – Camlost May 03 '20 at 03:52
0

It sounds like you might be struggling to understand where state should live in your application.

The example you mentioned in the comments, Nike and the "SKUsSizes" is likely getting that data from a backend API. Not by accessing a variable on another "web page".

This would be easier to explain if you had a more concrete example but say you need the page b123 to know how many products there are for a123.

The b123 page would make an HTTP request to your backend API requesting details about a123 which may include the number of products.

This data would be stored in a database.

On the other hand, if the product count is coming from some user action that happens over on a123 and you want to see that state change on page b123 then using local storage as suggested in another answer might be one way to solve that problem.

Although it's probably a better idea to use a framework like React, Vue or Angular and use the state management options they have instead of trying to figure out how to do this from scratch.

Dan Fletcher
  • 1,099
  • 11
  • 29