WEB调用JavaScript的方式

时光怂恿深爱的人放手 提交于 2019-12-10 01:46:53

概括、在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!"); 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!