Global对象 全局的、终极的、兜底的对象(不存在的) 所有在全局作用域内定义的属性和方法,都是Global对象的属性
常用的几个方法:encodeURI 、encodeURIComponent、decodeURI 、decodeURIComponent、escape、unescape 、eval、parseInt、parseFloat、isNaN
uri编码
encodeURI 、
encodeURIComponent、
以上方法用于对URI(通用资源标识符)进行编码,以便发送给浏览器。浏览器不能理解和接收一些特殊字符,比如:空格,所以这两个方法用UTF-8编码替换URI中的无效字符以使浏览器能够识别。
encodeURI(): 主要用于整个URI,不会对本身属于URI的特殊字符进行编码,如冒号,正斜杠、问号、井号
encodeURIComponent() : 任何不标准的字符都会进行转码,常用
uri解码:
decodeURI 、
decodeURIComponent、
示例:
//encodeURI 、encodeURIComponent
var uri = 'http://www.baidu.com cn';
var str1 = encodeURI(uri); //://不会进行转码
var str2 = encodeURIComponent(uri); //任何不标准的字符都会进行转码,常用
alert(str1); //http://www.baidu.com%20cn
alert(str2); //http%3A%2F%2Fwww.baidu.com%20cn
//decodeURI 、decodeURIComponent
alert(decodeURI(str1)); //http://www.baidu.com cn
alert(decodeURIComponent(str2)); //http://www.baidu.com cn
escape unescape 对中文进行转码、解码
var str1 = “一眼万年”;
alert(escape(str1)); //%u4E00%u773C%u4E07%u5E74
alert(unescape(escape(str1))); //一眼万年
eval 类似无形的JavaScript解析器
var str1 = "var a = 10; var b = 20;";
eval(str1); //传入字符串
alert(a+b);
//字符串对象
var str = "{name:'z3',age:20}";
var obj = eval("("+str+")"); //将字符串转换为对象
alert(obj.name);
parseInt、parseFloat
var num1 = parseInt("5");
var num2 = parseFloat("12.6");
alert(num1 + "||"+num2); // 5 12.6
alert(typeof num1); //number
alert(typeof num2); //number
isNaN是否不是一个数:不是number为true,是number为false.
在JS里面只有NaN不等于自己本身
var num1 = "10";
var num2 = "abc"
alert(isNaN(num1)); //false
alert(isNaN(num2)); //true
Date日期类型
var date = new Date();
alert(date);
//获取年
alert(date.getFullYear());
//获取月份
alert(date.getMonth()+1);
//获取日期
alert(date.getDate());
//获取时
alert(date.getHours())
//获取分
alert(date.getMinutes());
//获取秒
alert(date.getSeconds())
//星期
alert(date.getDay());
//获取当前时间的毫秒数
alert(date.getTime());