0

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

11
  • 2
    What do you mean by "The div is also set by a JS-file."? Commented Jul 12, 2012 at 17:42
  • 1
    @woz I'm guessing it means that #infowindow_content is created through js Commented Jul 12, 2012 at 17:43
  • 1
    What do you mean by "it still don't work"?? 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. 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. Commented Jul 12, 2012 at 17:45

3 Answers 3

5

Try this:

setTimeout(function() {
 $(document).ready(function() { 
     $('#infowindow_content').html('test');
 });
}, 20);
Sign up to request clarification or add additional context in comments.

5 Comments

If the somewhat cryptic allusion to the "JS-file" means that there's another script somewhere overwriting the work of the code in the OP, then this could definitely help.
This code worked. But I don't know why I need something like this. What's the reason or what I'm doing wrong?
@testing since the div is being added a-la javascript, there is no load or ready event for it. However if you wait 20 ms for all the js to execute then jQuery can find the div that your other js created
@qwertymk: The div is already there in HTML as placeholder. So it should be found I mean. The external JS file can set the content with document.getElementById('infowindow_content').innerHTML = 'test' and than I try with jQuery to set the content again. I also tried the Javascript way with the same results. But I think your solution is the only one which works.
FYI: Your code only works in between $(document).ready(function() {.
1

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

Comments

0

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

3 Comments

Common wisdom is to put the scripts at the end of the <body> so that their evaluation doesn't delay page rendering.
Reason for this is that it allows all of the DOM to be parsed before it tries to manipulate it, and overall improves loading time.
Oh sorry; the </body> in your answer was hidden because it was unquoted :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.