<script > 有 6 个属性:
async 对外部脚本有效, 可以用来异步操作, 下载东西, 不妨碍其他操作.
charset 为 src 属性指定字符集.
defer 表示脚本延迟, 等到文档被解析显示后才执行.
language 已废弃.
src 表示执行外部文件.
type 表示使用脚本语言的类型.
- <script type="text/javascript" src="dashu.js">
- </script>
- <script src="dashu.js">
- </script>
- <head>
- <script>
- </script>
- </head>
- <body>
- <script>
- </script>
- </body>
脚本会被延迟到整个页面解析完毕后运行, 不会影响页面结构:
<script defer="defer"></script>
type 编写代码使用的脚本语言的内容类型.
- <script type="text/javascript">
- function hello() {
- alert("hello");
- }
- </script>
- // 引用外部文件
- <script type="text/javascript" src="example.js"></script>
defer 表示脚本会被延迟到整个页面加载完毕后运行.
- <!DOCTYPE html>
- <HTML>
- <head>
- <title>
- </title>
- <script type="text/javascript" defer="defer" src="">
- </script>
- <script type="text/javascript" defer="defer" src="">
- </script>
- </head>
- <body>
- </body>
- </HTML>
异步脚本使用 async 属性
- <!DOCTYPE HTML>
- <HTML>
- <head>
- <title>
- </title>
- <script type="text/javascript" async src="">
- </script>
- </head>
- <body>
- </body>
- </HTML>
xhtml 可扩展超文本标记语言
- <script type="text/javascript">
- function compare(a,b){
- if(a<b){
- }else{
- }
- }
- </script>
- <noscript > 元素, 浏览器不支持脚本, 浏览器支持脚本, 但脚本被禁用.
- <HTML>
- <head>
- <title></title>
- <script type="text/javascript" defer="defer" src=""></script>
- </head>
- <body>
- <noscript>
- <p > 需要浏览器启用 JavaScript</p>
- </noscript>
- </body>
- </HTML>
JavaScript 的使用要用 < script > 元素, 外部的文件使用 src 指向相应的文件,<script > 会按照他们在页面的先后依次被解析, defer 属性可以让当前脚本不必等待其他脚本, 使用 < noscript > 元素可以指定在不支持脚本的浏览器中显示替代的内容.
基本语法, 数据类型:
数据类型
操作符
语句
JavaScript 区分大小写, 标识符, 第一个字符以字母, 下划线或一个美元符号, 其他可以是字母, 下划线, 美元符号, 或数字.
不能把关键字, 保留字等用作标识符.
严格模式:
"use strict";
关键字:
- break,case,catch,continue,default,delete,
- do,else,finally,for,function,if,in,
- instanceof,new,return,switch,this,throw,try,
- typeof,var,void,while,with,
保留字:
- abstract,enum,int,short,
- boolean,export,interfacee,static,
- byte,char,class,const,double,
- extends,final,float,goto,implements,import,
- interface,long,native,package,private,protected,public
- short,static,super,synchronized,throws,volatile
- function test() {
- var msg = "hello"; // 局部变量
- }
- test();
- function test() {
- msg = "hello"; // 全局变量
- }
- test();
数据类型:
Undefined, null, boolean, number, string, object
undefined 未定义;
boolean 布尔值;
string 字符串;
number 数值;
object 对象或 null;
function 为函数.
type 操作符用来判断变量的数据类型.
undefined 类型
- var message;
- console.log(message == undefined); // true;
结言
好了, 欢迎在留言区留言, 与大家分享你的经验和心得.
感谢你学习今天的内容, 如果你觉得这篇文章对你有帮助的话, 也欢迎把它分享给更多的朋友, 感谢.
来源: http://www.jianshu.com/p/6dd3a9131fdc