0

When pressing a button on page1.html I want to open page2.html in new window and copy some data from page1.html to page2.html.

I tried running open_copy() on page1.html

<button id="manual" onclick="open_copy()" type="button" class="btn-primary">copy age</button>

Uncaught TypeError: Cannot set property 'value' of null

I try using local storage, it works but it isn't nice...

 function open_copy()
{
  var age=document.getElementById('age').value;
  var n= window.open('page2.html');
  n.focus();  
  n.document.getElementById('age').value=0.0001;
}

I want to copy age data from page1 to page2.html

1 Answers1

0

In your page1, set the data you want to pass to a local storage like this

var age=document.getElementById('age').value;
localStorage.setItem('age', age);

then in your page2, get your data from the local storage like this

var age = localStorage.getItem('age');

SYNTAX

To Set

localStorage.setItem('key', 'value')

To get

localStorage.getItem('key')

JkAlombro
  • 1,696
  • 1
  • 16
  • 30
  • Thanks Jk, I use to activate localStorage.getItem('key') on the onload of pag2.html. Is there a way to do it from page1.html, on open_copy() ? also do you knoe how to use the POST option? – Oren Ben-Harim Jun 14 '19 at 10:13
  • There's no way to do that on page1. localstorage is useless if you can load the data from page1 to page2 in the first place. I do know how to use POST option but I suggest you stick to localstorage. Post option is 10x more complicated than localstorage. You'll need to create API and database for that. – JkAlombro Jun 17 '19 at 02:00