user3771854
user3771854

Reputation: 97

Auto load Javascript

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>');

Upvotes: 3

Views: 26327

Answers (5)

0x777
0x777

Reputation: 945

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.

Upvotes: 0

Fizzix
Fizzix

Reputation: 24375

Load a function after your body loads, like so:

<body onload="startgame();">

Then create the function

function startgame() {
    alert('Body has loaded!');
}

Upvotes: 1

algorhythm
algorhythm

Reputation: 8728

<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.

Upvotes: 2

user3221295
user3221295

Reputation:

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;}

Upvotes: 1

ctwheels
ctwheels

Reputation: 22817

What about an on load function?

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

JavaScript: window.onload = your function here

Upvotes: 0

Related Questions