Tuesday, May 28, 2024
 Popular · Latest · Hot · Upcoming
91
rated 0 times [  94] [ 3]  / answers: 1 / hits: 17074  / 11 Years ago, thu, march 28, 2013, 12:00:00

My webpage has the following code:



<html>
<head>
<title>This is test Page</title>

<script language=javascript type=text/javascript>

document.getElementById(msg1).innerHTML = document.URL.toString();
</script>

</head>
<body>

<div class=sss>
<p id=msg1></p>
</div>


</body>
</html>


As you now at the time the script executes the div doesn't exist but I want to put my JavaScript code only In the <head> tag and I won't put it in middle of HTML code.



But this code only works when I put the <script> tag after the <div> tag.
I use VS2010 and firefox 19.0.1



Is there anyway to put code in <head> tag?


More From » html

 Answers
14

Your script relies on the DOM being ready, so you need to execute that function call only after the DOM is ready.



<script language=javascript type=text/javascript>

window.onload = function() {
document.getElementById(msg1).innerHTML = document.URL.toString();
}

</script>

[#79281] Wednesday, March 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aileent

Total Points: 556
Total Questions: 107
Total Answers: 101

Location: Croatia
Member since Fri, Sep 11, 2020
4 Years ago
;