自动加载 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24396035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:42:36  来源:igfitidea点击:

Auto load Javascript

javascriptjqueryajax

提问by user3771854

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.

我有一个 Flash 游戏网站。当我进入游戏时,我有一个标记为“玩游戏”的按钮。用户需要按下此按钮才能开始游戏。但我不希望用户必须使用按钮来启动游戏。我可以如何改变以及改变什么?这是我网站上的 javascript 。

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

采纳答案by Fizzix

In your HTML you can try:

在您的 HTML 中,您可以尝试:

<body onload="startgame();">

Or in your JS file you can try:

或者在您的 JS 文件中,您可以尝试:

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

回答by algorhythm

<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()

有关 和 之间的区别window loaddom ready请参见此处:window.onload vs $(document).ready()

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

简而言之:window.onload 在 dom 准备好之后出现,例如所有图像都已加载。

回答by Fizzix

Load a function after your body loads, like so:

在您的身体加载后加载一个函数,如下所示:

<body onload="startgame();">

Then create the function

然后创建函数

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

回答by John-Perez

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

如果您已经有一个window.onload函数,这很有用。

回答by ctwheels

What about an on load function?

加载功能怎么样?

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

Jquery: JQuery(document).ready(你的函数在这里)

JavaScript: window.onload = your function here

JavaScript: window.onload = 你的函数在这里