Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 15, 2023 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/dcd08c6ac3e0ff60859d58d0b3f1b3ae to your computer and use it in GitHub Desktop.
Save code-boxx/dcd08c6ac3e0ff60859d58d0b3f1b3ae to your computer and use it in GitHub Desktop.
Javascript Pass Variables Between Pages

JAVASCRIPT PASS VARIABLES BETWEEN PAGES

https://code-boxx.com/pass-variables-between-pages-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Query String Part 1</title>
<script>
function go () {
// (A) VARIABLES TO PASS
var one = "Foo Bar",
two = ["Hello", "World"];
// (B) URL PARAMETERS
var params = new URLSearchParams();
params.append("one", one);
params.append("two", JSON.stringify(two));
// (C) REDIRECT
var url = "1b-query.html?" + params.toString();
location.href = url;
}
</script>
</head>
<body>
<input type="button" value="GO" onclick="go()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Query String Part 2</title>
<script>
function get () {
// (A) GET THE PARAMETERS
var params = new URLSearchParams(window.location.search),
one = params.get("one"),
two = JSON.parse(params.get("two"));
// (B) IT WORKS!
console.log(one);
console.log(two);
}
</script>
</head>
<body>
<input type="button" value="GET" onclick="get()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Session Storage Part 1</title>
<script>
function go () {
// (A) VARIABLES TO PASS
var one = "Foo Bar",
two = ["Hello", "World"];
// (B) SAVE TO SESSION STORAGE
sessionStorage.setItem("one", one);
sessionStorage.setItem("two", JSON.stringify(two));
// (C) REDIRECT
location.href = "2b-session-storage.html";
}
</script>
</head>
<body>
<input type="button" value="GO" onclick="go()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Session Storage Part 2</title>
<script>
function get () {
// (A) GET FROM SESSION STORAGE
var one = sessionStorage.getItem("one"),
two = JSON.parse(sessionStorage.getItem("two"));
// (B) IT WORKS!
console.log(one);
console.log(two);
// (C) CLEAR SESSION STORAGE
// sessionStorage.removeItem("KEY");
// sessionStorage.clear();
}
</script>
</head>
<body>
<input type="button" value="GET" onclick="get()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Local Storage Part 1</title>
<script>
function go () {
// (A) VARIABLES TO PASS
var one = "Foo Bar",
two = ["Hello", "World"];
// (B) SAVE TO LOCAL STORAGE
localStorage.setItem("one", one);
localStorage.setItem("two", JSON.stringify(two));
// (C) REDIRECT
location.href = "3b-local-storage.html";
}
</script>
</head>
<body>
<input type="button" value="GO" onclick="go()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Local Storage Part 2</title>
<script>
function get () {
// (A) GET FROM LOCAL STORAGE
var one = localStorage.getItem("one"),
two = JSON.parse(localStorage.getItem("two"));
// (B) IT WORKS!
console.log(one);
console.log(two);
// (C) CLEAR LOCAL STORAGE
// localStorage.removeItem("KEY");
// localStorage.clear();
}
</script>
</head>
<body>
<input type="button" value="GET" onclick="get()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>New Window Part 1</title>
<script>
function go () {
// (A) VARIABLES TO PASS
var one = "Foo Bar",
two = ["Hello", "World"];
// (B) OPEN NEW WINDOW - JUST PASS VARIABLES TO NEW WINDOW
var newwin = window.open("5b-window.html");
newwin.onload = () => {
newwin.one = one;
newwin.two = two;
};
}
</script>
</head>
<body>
<input type="button" value="GO" onclick="go()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>New Window Part 2</title>
<script>
function get () {
console.log(one);
console.log(two);
}
</script>
</head>
<body>
<input type="button" value="GET" onclick="get()">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment