newbie
newbie

Reputation: 11

How to change a variable in the JavaScript without loading the JavaScript content?

I have a typing web game written using JS. The JS contains all the necessary definition and algorithm of the game. There is a global variable called game_mode. By altering this variable to either 0, 1 or 2, I can load different game modes on the webpage. My difficulty is, I can't figure out a way where I can edit game_mode before I load the JS content using HTML. If I load the JS, the JS already starts running and thus the game starts, using the game_mode hard-coded. Is there anyway to do so?

Upvotes: 0

Views: 77

Answers (1)

Jeff Sloyer
Jeff Sloyer

Reputation: 4964

Take all your Javascript code that you have written and put into a file, example game.js. In your HTML you can import/reference the Javascript. Before referencing it set the variable game_mode to the desired value.

See below.

index.html

<script>
    var game_mode = 2;
</script>
<script src="/game.js"> </script>

Upvotes: 2

Related Questions