最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

HTML onload not calling JavaScript function - Stack Overflow

programmeradmin2浏览0评论

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.

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.

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.

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.

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.

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.

Share Improve this question edited Sep 3, 2012 at 16:32 Dr.Molle 117k16 gold badges199 silver badges206 bronze badges asked Sep 3, 2012 at 16:07 barryedmundbarryedmund 6122 gold badges9 silver badges18 bronze badges 2
  • 6 Do you get any errors if you turn on JavaScript debugging in your browser? – Richard Ev Commented Sep 3, 2012 at 16:10
  • Ah, yes. Just turned on the JS debugger and it seems that there is an uncaught syntaxError. This may be the result of the format changing on a CSV the site fetches. Now to find the change. Thanks for the nudge in the right direction. – barryedmund Commented Sep 3, 2012 at 16:32
Add a ment  | 

4 Answers 4

Reset to default 1

Make sure that your script tag is correct.

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

It should be:

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

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.

for body on load, use this.

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

or it can be shortened even further to

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

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.

The issue with the uncaught syntax error (see ments 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
 $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.

发布评论

评论列表(0)

  1. 暂无评论