I have two html pages that two javascript functions.
page1.html
<a href="page2.html" onclick="ex1()">link1</a>
page2.html
<a href="#" onclick="ex2()">link2</a>
jscript1.js
var abc;
function ex1() {
abc='some text';
console.log(abc);
}
function ex2() {
console.log(abc);
}
In the console, I’m getting undefined when inside the ex2() function. What am I doing wrong here?
You’re going to another page, where your abc variable isn’t defined.
You need to render your variable persistent from one page to the other.
One solution is to use the localStorage :