How does JS work with multi page website _ like this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How does JS work with multi page website _ like this?

I got: index.html page2.html script.js page2.js So, obviously, index.html and script.js go together. This is kind of a game I’m trying to make btw. In the script.JS, I have a variable called “result”; Based on action the result increases and decreases. If (result > 5 ) { aar a = document.createElement(“a”); a.innerText = “Reveal; a.href = “page2.html”; aContainer.appendChild(a); prize.innerHTML = “cheese”;; } else if(result == 5) { prize.innerHTML = “bread”; } _ prize is inside page2.html, so how am supposed to test 5, and use it on a different page (page2); ——- I’m tired right now, I can’t continue typing Let me know if you don’t understand what I’m saying. -

8th Dec 2019, 7:03 AM
Ginfio
Ginfio - avatar
7 Answers
+ 1
You can use Web Storage in Html. Check here what is Web Storage and how we can use it. https://www.sololearn.com/discuss/1712946/?ref=app https://www.sololearn.com/discuss/1712406/?ref=app
8th Dec 2019, 10:06 AM
A͢J
A͢J - avatar
+ 1
Ginfio I understood your problem. One Js file in one Html and other js file in other Html then how you can call variable from one js to another js without including the first js file in second html. Think once then let me know that does it make sense or not.
8th Dec 2019, 4:48 PM
A͢J
A͢J - avatar
+ 1
Ginfio They use database or what I told you Web Storage or they also use cookies some times. Yes we can use same JS or CSS in all pages but we cannot read one variable from one JS to another JS if both are in different Html. See web storage and cookies stores data in browser and our different js file is also on the same browser so we can access value from one JS to another JS through these two.
8th Dec 2019, 5:34 PM
A͢J
A͢J - avatar
+ 1
🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ! Ok. Thanks for ur help. I appriciate it! -
8th Dec 2019, 6:17 PM
Ginfio
Ginfio - avatar
0
🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ! I dont’ understand y I’d be using storage though. I’m trying to use a variable from the script.JS on the other js page (page2.js). Or to better understand, look at it like this: script.js is linked to index.html; page2.js is linked to page2.js; Inside script.js I have a score variable page2.html is to display the score. let’s say inside page2.html I have: Your Score is <span class = “span”> # </span> Now how can I use that score variable that’s inside script.js like this .span.innerHTML = score; Let me know if that doesn’t make sense.
8th Dec 2019, 4:31 PM
Ginfio
Ginfio - avatar
0
🅰🅹 - ɪ'ᴍ ᴄʀɪᴍɪɴᴀʟʟʏ ɢᴏᴏᴅ! No, it doesn’t make sense at all. I was just wondering if there was a way to do it... So, does it mean I need to display the Score on the same page? But on actual websites that have multiple pages (Home, about, service), do they use the same JS for all the pages, or do each page have their own JS?
8th Dec 2019, 5:18 PM
Ginfio
Ginfio - avatar
0
Ginfio Welcome. I am here to help everyone.
8th Dec 2019, 6:39 PM
A͢J
A͢J - avatar