I am new to coding, and I am trying to develop some sort of a simple board game.
I have two pages, on the first one, I have a list where one should choose the number of players, then click on a button that will run a function to open a second page and display the number of players.
The problem is when the function is runnig, it seems to stop at window.location, which makes sense. Is there a way that my function could keep running even after the page has changed ?
Below are my two pages and my js code simplified to include only the element that are part of my problem.
function chooseNbrPlayers(){
window.location.href = "page2.html";
document.getElementById("demo").innerHTML = document.getElementById("selectNbrPlayers").selectedIndex;
var nbrPlayers = document.getElementById("selectNbrPlayers").selectedIndex;
}
<html>
<head>
<title>page1</title>
<p><button onclick="chooseNbrPlayers()">Play</button></p>
<body>
<form><select id="selectNbrPlayers">
<option></option>
<option>1 player</option>
<option>2 players</option>
<option>3 players</option>
<option>4 players</option>
</select>
</form>
</body>
<script src=script.js></script>
</head>
</html>
<html>
<head>
<title>page2</title>
<p>There are <span id="demo"></span> players.</p>
<script src=script.js></script>
</head>
</html>