> Html and javascript question?

Html and javascript question?

Posted at: 2014-12-18 
Just put the game's main code inside its own function. Then call that using a link, or a button's onclick:

Start Game

You need to set up a click event and put your whole game script into it. It could look like this:

in html:

Click here to start game.


in JS:

function prep() {

document.getElementById('start').onclick = startGame;

}

window.onload = prep;

function startGame() {

//code to start the game

}

The window.onload is needed because the code that sets the onclick handler (inside the prep() function) may not find the div to set the click event on, since the Javascript may run before the HTML is completely parsed and rendered. onload runs after the HTML is completely loaded. Then when someone clicks on the div, your game code will run.

I have a webpage coded with all HTML and CSS. I wrote a small javascript game (very basic) and I put it in my website. The problem I am having is that the javascript file runs automatically (and prompts you to play the game) when I open my site. I want to make it to where the javascript file will not run unless the person visiting my site clicks on something (i.e. a button/word/link... whatever I choose) to make it run. Can this be done?

Thanks