您的位置:首页 > Web前端 > JavaScript

WEB调用JavaScript的方式

2011-09-27 11:32 204 查看
概括、在WEB中一共有四种方式可以引用js代码。

  1、<script>直接引用代码</script>

  2、<script src=xxx.js></script> //间接引用代码

  3、<script defer>间接延迟引用代码</script>

  4、javascipt:在地址栏中直接执行代码

示例:

<html>
<head>
<!--first run-->
<script>
alert("embedded script is in the head!");
</script>
<!--second run-->
<script src=out.js></script>
<script defer>alert("delay script in the head!");</script>
<!--在IE下是加载完body才运行,在firefox下是加载完head就运行-->
</head>

<body>
<!--third run-->
<script>
alert("embedded script is in the body!");
</script>
<!--fourth run-->
<script src=out.js></script>
<input type="button" value="button1" onclick='alert("button1 is clicked!")'/>
<!--所有的事件都以on开头-->
</body>
</html>
<!--还可以在地址栏中输入JavaScript代码,javascript:alert("script in the url!");-->


out.js

alert("out script!");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: