jquery - Execute JavaScript code at the end when the HTML has been loaded - Stack Overflow

I want to execute a function at the end when the HTML has been loaded. I tried it with onload without s

I want to execute a function at the end when the HTML has been loaded. I tried it with onload without success. I also tried it with ready, but it still doesn’t work. Here is my code. This is again placed in the header:

<script type="text/javascript">
    $(document).ready(function() {
        $('#infowindow_content').html('test');
    });
</script>

The div is also set by an external JavaScript file. Content:

window.onload = initialize;

function initialize() {
    document.getElementById('infowindow_content').innerHTML = 'testa';
}

It is included the following way before the closing body tag:

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

I tried to place the above code before the closing body tag, but currently I have no idea why this doesn't work (the content isn't changed by my JavaScript code). If I execute it on the console afterwards everything works fine.

Solution:

I set a configuration parameter (language) in the HTML file. In the JavaScript file I ask for this value and depending on the value I define another content. Sometimes it could be so simple ...

I want to execute a function at the end when the HTML has been loaded. I tried it with onload without success. I also tried it with ready, but it still doesn’t work. Here is my code. This is again placed in the header:

<script type="text/javascript">
    $(document).ready(function() {
        $('#infowindow_content').html('test');
    });
</script>

The div is also set by an external JavaScript file. Content:

window.onload = initialize;

function initialize() {
    document.getElementById('infowindow_content').innerHTML = 'testa';
}

It is included the following way before the closing body tag:

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

I tried to place the above code before the closing body tag, but currently I have no idea why this doesn't work (the content isn't changed by my JavaScript code). If I execute it on the console afterwards everything works fine.

Solution:

I set a configuration parameter (language) in the HTML file. In the JavaScript file I ask for this value and depending on the value I define another content. Sometimes it could be so simple ...

Share Improve this question edited Nov 21, 2021 at 13:18 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jul 12, 2012 at 17:39 testingtesting 20.3k54 gold badges243 silver badges432 bronze badges 11
  • 2 What do you mean by "The div is also set by a JS-file."? – woz Commented Jul 12, 2012 at 17:42
  • 1 @woz I'm guessing it means that #infowindow_content is created through js – qwertymk Commented Jul 12, 2012 at 17:43
  • 1 What do you mean by "it still don't work"?? – Pointy Commented Jul 12, 2012 at 17:43
  • @woz: I have before the closing tag included a JS-file with which the content of the div is set. Now I want to overwrite this content. – testing Commented Jul 12, 2012 at 17:43
  • @Pointy: The content isn't changed. With the console I can easily change the content, but this should be made automatically. – testing Commented Jul 12, 2012 at 17:45
 |  Show 6 more ments

3 Answers 3

Reset to default 5

Try this:

setTimeout(function() {
 $(document).ready(function() { 
     $('#infowindow_content').html('test');
 });
}, 20);

I don't know the jQuery equivalent but try the native JS.

Since the <body> has the most HTML & loads after <head>...

document.body.onload=function(){
    yourFunction(args)
}
<body onload="yourFunction(args)">...</body>

Or maybe the window object, since it's the root of every webpage DOM...

window.onload=function(){
    yourFunction(args)
}

Always place DOM manipulating code directly before your </body> tag. JavaScript in the header should only be called to libraries, such as jQuery.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744937430a4602113.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信