javacript在哪里有区别 [英] is there diffrent where the javacript

查看:92
本文介绍了javacript在哪里有区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用了javascript,我们可以将javascript代码放在head标签中,例如< head>< code is here> </head>或在body标签中,例如< body<代码在此处</body并将其放在</html>之后,就像</html><代码在此处> ,现在有什么不同吗?:confused:

当我们使用此javascript时,在页面第一次加载页面时,此javascript是否会执行(不使用onload事件)

我该怎么办?

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?

推荐答案

不确定它在技术上是否有效,但是它在IE8中对我有效:
Not sure if it''s technically valid, but it works for me in IE8:
<html>
<body>
	This is the body.
</body>
</html>
<script type="text/javascript">
	alert("hello");
</script>


您的最后一部分根本不清楚.关于上一部分:
您可以将JavaScript代码放在页面中的任何位置,通常在html标签开始之前或html标签结束之后.
现在,区别在于加载时-
1.如果您不使用任何与页面加载相关的脚本,则可以将其放在html标记的末尾.这将首先呈现页面,然后加载javascript.因此,提高了UI性能.
2.如果将Javascript放在HTML标记之前,则将首先下载它们,然后呈现UI.

这与您首先需要执行/下载/渲染的内容有关.如果页面上有一个onload javascript事件,则应将JS放在HTML标记之前,以使执行平稳.
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.


这篇关于javacript在哪里有区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆