Click here to Skip to main content
15,904,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
there we use the javascript ,we can place the javascript code in the head tag, like <head><code is here> </head> or in the body tag,like <body><code is here></body> and can put it behind the </html> ,like </html><code is here> , and now is there have the diffrence of this?:confused:

and when we use this javascript,in the page , load page first time,is this javascript will excute (don't use the onload event)

How can i do?
Posted

Not sure if it's technically valid, but it works for me in IE8:
HTML
<html>
<body>
	This is the body.
</body>
</html>
<script type="text/javascript">
	alert("hello");
</script>
 
Share this answer
 
v2
Comments
AspDotNetDev 8-Aug-10 4:52am    
Also, the "difference" between placing this script here and placing it somewhere else is that it will execute at a later time.
lovejun 8-Aug-10 9:35am    
thanks,this can be test in my local pc~
lovejun 8-Aug-10 9:35am    
Reason for my vote of 5
give me a solotion
Your last part is not clear at all. About the previous part:
You can put the JavaScript code anywhere in the page, generally before html tag starts or after html tag ends.
Now the difference lies while loading -
1. If you don't use any script that is related to page load then you can put it at the end after html tag. This will render the page first and then load the javascript. So, a UI performance is increased.
2. If you put the Javascript just before HTML tag then they will first be downloaded and then the UI will be rendered.

It's all about what you need to be executed/downloaded/rendered first. If you have a onload javascript event of the page then you should put the JS before HTML tag to have a smooth execution.
 
Share this answer
 
Comments
lovejun 8-Aug-10 11:03am    
there the last part meaning:when the page renderd first time,the javascript excute or not?have the diffrence of the alert('') and the , or to say this javascript is only downloaded not excute .thanks
lovejun 8-Aug-10 11:04am    
Reason for my vote of 5
good ansers
Sandeep Mewara 8-Aug-10 11:13am    
If you bind any Javascript on page onload then it will be executed. Methods are not executed. They are just downloaded and present on the page. If you put an "alert('Sandeep')" in your JS directly (not in any method) then it will get executed and you will see an alert box when page renders. To keep it and not execute it, put this in a method that you will call later based on certain event.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900