HTML onload 不调用 JavaScript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12251152/
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-08-23 07:31:47  来源:igfitidea点击:

HTML onload not calling JavaScript function

javascripthtmlonload

提问by barryedmund

I have looked at other questions similar to this, but I haven't found an answer.

我看过其他类似的问题,但我还没有找到答案。

My <body onload="doStuff()">has stopped calling the doStuff() JavaScript function. I have tried replacing <body onload="doStuff()">with <body onload="alert('Test');">and that creates the alert successfully.

<body onload="doStuff()">已停止调用 doStuff() JavaScript 函数。我试过替换<body onload="doStuff()"><body onload="alert('Test');">并成功创建警报。

Then I tried putting that same alert just inside the doStuff() function (and reverting the onload to call doStuff()), but the alert did not appear.

然后我尝试将相同的警报放在 doStuff() 函数中(并恢复 onload 以调用 doStuff()),但警报没有出现。

Are there any reasons why this would happen? Also, it may be relevant to note that I am almost certain that I did not make any code changes in between this working and it not working (you may not believe that, but it's true); however, I did delete a sub-folder from the server that contained a Joomla installation.

有什么原因会导致这种情况发生吗?此外,可能需要注意的是,我几乎可以肯定,在此工作和不工作之间我没有进行任何代码更改(您可能不相信,但这是真的);但是,我确实从包含 Joomla 安装的服务器中删除了一个子文件夹。

回答by ThreaT

Make sure that your script tag is correct.

确保您的脚本标签正确。

<script src="myscript.js" />will cause <body onload="...">...</body>to fail.

<script src="myscript.js" />会导致<body onload="...">...</body>失败。

It should be:

它应该是:

<script src="myscript.js" type="text/javascript"></script>

回答by Rico

For whatever reason in firefox, my Scripts declared in the body of the page was preventing inline calls from firing. I moved my script tags to the header and then it worked.

无论出于何种原因,在 Firefox 中,我在页面正文中声明的脚本都阻止了内联调用的触发。我将我的脚本标签移到了标题中,然后它就起作用了。

回答by Graeme Leighfield

Try to move away from inline calls and utilise jQuery as it was intended. Its really good working practice, (not to mention easier to debug) by keeping your style, and script logic separate.

尝试远离内联调用并按预期使用 jQuery。通过保持您的风格和脚本逻辑分开,这是非常好的工作实践(更不用说更容易调试)。

for body on load, use this.

对于负载的身体,使用这个。

$(document).ready(function () {
    doStuff();
});

or it can be shortened even further to

或者它可以进一步缩短为

$(function () {
    doStuff();
});

回答by barryedmund

The issue with the uncaught syntax error (see comments in original post) was that, when I was converting a PHP array into a JavaScript array, something was going wrong, i.e., a weird character was being appended. I solved this by replacing my DIY PHP-array-to-JS-array code with this code:

未捕获的语法错误(请参阅原始帖子中的评论)的问题在于,当我将 PHP 数组转换为 JavaScript 数组时,出现了一些错误,即附加了一个奇怪的字符。我通过用以下代码替换我的 DIY PHP-array-to-JS-array 代码解决了这个问题:

<?php
 $js_array = json_encode($resultsArray);
 echo "var jsResultsArray = ". $js_array . ";\n";
?>

This isn't really connected to the headline question of the post, but it was the root problem.

这与帖子的标题问题并没有真正的联系,但这是根本问题。