Open up your developer tools and watch the network tab.
When you click the submit button, it will:
- Run your function.
- Reload the page
You need to prevent the page from submitting.
There are two approaches.
Approach 1: Prevent the submit
<html>
<head>
<script>
var Experience = 1;
function scenario1() {
// Shorthand way to write it
window.Experience++;
alert(window.Experience);
}
</script>
</head>
<body>
<form>
<input type="submit" onclick="scenario1(); return false">
</form>
</body>
</html>
Approach 2: Use a button that doesn't submit
<html>
<head>
<script>
var Experience = 1;
function scenario1() {
// Shorthand way to write it
window.Experience++;
alert(window.Experience);
}
</script>
</head>
<body>
<form>
<button type="button" onclick="scenario1()">Do it</button>
</form>
</body>
</html>
Extra notes
Lots of global variables is generally a pretty bad idea. If this is for a game of some sort, you'd be better putting everything into a player object.
var player = {
experience: 0,
name: "Just some guy"
};
// Grant experience
player.experience += 10;