javascript code not work in HEAD tag
Solution 1:
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>
Solution 2:
Your script uses a DOM element and therefore must run after the DOM is loaded. You can do that by using this function:
$(document).ready(function(){
//code here
}
Solution 3:
The various tags in your HTML page are loaded and processed in the order in which they appear on the page. Your <script>
tag is executed immediately when it is parsed in the <head>
. This is before the <body>
and the elements inside the <body>
are parsed. So, the script tries to reference an element that is not defined at the time it is executed.