WEB调用JavaScript的方式

来源:互联网 发布:linux reboot重启失败 编辑:程序博客网 时间:2024/05/23 15:43

概括、在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!"); 



原创粉丝点击