3

I have a flash games website. When I enter a game I have a button labeled "Play Game". The user needs to push this button to start the game. But I don't want users to have to start the game by using a button. How and what can I change? Here is the javascript from my website.

}
function sgame() {
var x=document.getElementById('gamefirst').style;
var y=document.getElementById('gamesecond').style;
if(x.display=='block') { x.display='none'; y.display='block'; }
else { x.display='block'; y.display='none'; }
}

and maybe

function swf(src, width, height) {
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="733" height="550"  id="currentGame">');
    document.write('<param name="movie" value="' + src + '">');
    document.write('<param name="quality" value="high">');
    document.write('<embed src="' + src + '" id="currentEmbedGame" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="733" height="550" menu="0"></embed>');
    document.write('</object>');
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user3771854
  • 97
  • 1
  • 1
  • 6

5 Answers5

2
<script type="text/javascript">
    window.onload = function(event) {
        // your code here
    };

    // equivalent with jQuery (when window loads)

    jQuery(window).load(function(event) {
        // your code here
    });

    // alternatively with jQuery (when DOM is ready)

    jQuery(document).ready(function(event) {
        // your code here
    });

    // you can also call a function if this <script>-block is reached (loaded) first time
    // place the <script>-block before body-tag is closing and you have nearly what DOM-ready does.
    someFunction();
</script>

For the difference between window loadand dom readysee here: window.onload vs $(document).ready()

For short: window.onload comes later after dom is ready an e.g. all images are loaded.

Community
  • 1
  • 1
algorhythm
  • 8,530
  • 3
  • 35
  • 47
1

In your HTML you can try:

<body onload="startgame();">

Or in your JS file you can try:

window.onload=function(){//whatever you need to do;}
1

Load a function after your body loads, like so:

<body onload="startgame();">

Then create the function

function startgame() {
    alert('Body has loaded!');
}
Fizzix
  • 23,679
  • 38
  • 110
  • 176
0

What about an on load function?

Jquery: JQuery(document).ready(your function here)

JavaScript: window.onload = your function here

ctwheels
  • 21,901
  • 9
  • 42
  • 77
  • where can I put this codes? can I use both or only one? – user3771854 Jun 24 '14 at 21:07
  • Put them before the function you want to run when the webpage is loaded. "Your function here" is placeholder for the actual function you wan loaded. Use one or the other, they are the same thing just one is JQuery and the other is JavaScript – ctwheels Jun 24 '14 at 21:10
0

Try the following, it's a piece of code that's executed on script load:

(
   function h(){ alert('H'); }
)();

This is useful if you already have a window.onload function.

0x777
  • 825
  • 8
  • 14