JAVASCRIPT

HOW TO PASS VARIABLES BETWEEN PAGES IN JAVASCRIPT

PAGE A - STORE DATA INTO SESSION sessionStorage.setItem("KEY", "VALUE"); location.href = "PAGE.HTML";

PAGE B - GET DATA FROM SESSION var data = sessionStorage.getItem("KEY");

SESSION STORAGE

01

PAGE A - STORE DATA INTO STORAGE localStorage.setItem("KEY", "VALUE"); location.href = "PAGE.HTML";

PAGE B - GET DATA FROM STORAGE var data = localStorage.getItem("KEY");

LOCAL STORAGE

02

PAGE A - URL PARAMETERS var params = new URLSearchParams(); params.append("KEY", "VALUE"); location.href = "PAGE.HTML" + params.toString();

PAGE B - PARSE PARAMS var params = new URLSearchParams (window.location.search); var data = params.get("KEY");

URL SEARCH PARAMETERS

03

PAGE A - STORE INTO COOKIE var p = new URLSearchParams(); p.append("KEY", "VALUE"); document.cookie = "pass=" + p.toString(); location.href = "PAGE.HTML";

COOKIE STORAGE

04

PAGE B - GET COOKIE var pass = document.cookie   .split("; ")   .find(row => row.startsWith("pass"))   .substring(5); var p = new URLSearchParams(pass); var data = p.get("KEY");

PAGE A - OPEN NEW WINDOW & PASS VARIABLES OVER var key = "VALUE"; var win = window.open("FILE.HTML"); win.onload = () => { win.key = key; };

PAGE B - JUST USE THE VARIABLES IN THE NEW WINDOW console.log(key);

OPEN NEW WINDOW

05