Hey, guys. I have a question.
I have a function that should work so that when I click on an image it opens another page and according to what image it sets a number, and I need to get the number out of the function but not through return.
Code:
let page = null;
var p = null;
function onClick1(image) {
p = image;
location.href = “indexx.html”;
}
page = p
In that indexx.html it should output me the value of page but the output is null
Can anybody give me some advice?
Hello,
Can you please share with us the whole code base ?
index.html : Create image
<div id="iimg"><br><img src="images/profil/1.jpg" onclick="onClick1(1)"><br></div>
<div id="iimg"><br><img src="images/profil/2.jpg" onclick="onClick1(2)"><br></div>
.....
js.js
let page = null;
var p = null;
function onClick1(image) {
p = image;
location.href = "indexx.html";
}
page = p
indexx.html: for now
console.log( page ) output is nill
Are you familiar with the concept of global scope?
Your page = p is written in the global scope and so p will always be null and so will page.
What I undersatnd from this is that you are trying to check the value of a variable defined in a JS file called js.js
inside the script tag in a html file called indexx.html
because if that is the case this will not work
Yes, that’s what I meant.
I’m just learning JS so I don’t know many things how to do it.
How could I do it?
Honestly, I do not know if such thing is doable or not because it is very uncommon use case but looking back at your JS code, I noticed something in the logic build up
let page = null;
var p = null;
function onClick1(image) {
p = image;
location.href = "indexx.html";
}
page = p
you assigned image
to p
inside the function then outside the function you assigned p
to page
, I think you wanted to store the new value of p
that it got inside the function in the page
variable but this will not work because the code inside the function will work independently from the code outside of it, page = p
is like saying page = null
which is already the case
As for the console.log( page )
if you want to see its value, I think you should add it to the function body