decodeURI()
decodeURI()
方法返回指定位置的字符串
<script type="text/javascript">
var test1="http://www.w3school.com.cn/My first/"
document.write(encodeURI(test1)+ "<br />")
document.write(decodeURI(test1))
</script>
输出:
http://www.w3school.com.cn/My%20first/
http://www.w3school.com.cn/My first/
decodeURIComponent()
decodeURIComponent()
函数可对 encodeURIComponent() 函数编码的 URI 进行解码。
<script type="text/javascript">
var test1="http://www.w3school.com.cn/My first/"
document.write(encodeURIComponent(test1)+ "<br />")
document.write(decodeURIComponent(test1))
</script>
输出:
http%3A%2F%2Fwww.w3school.com.cn%2FMy%20first%2F
http://www.w3school.com.cn/My first/
encodeURI()
encodeURI()
函数可把字符串作为 URI 进行编码。
<script type="text/javascript">
document.write(encodeURI("http://www.w3school.com.cn")+ "<br />")
document.write(encodeURI("http://www.w3school.com.cn/My first/"))
document.write(encodeURI(",/?:@&=+$#"))
</script>
输出:
http://www.w3school.com.cn
http://www.w3school.com.cn/My%20first/
,/?:@&=+$#
encodeURIComponent()
encodeURIComponent()
函数可把字符串作为 URI 组件进行编码。
<script type="text/javascript">
document.write(encodeURIComponent("http://www.w3school.com.cn"))
document.write("<br />")
document.write(encodeURIComponent("http://www.w3school.com.cn/p 1/"))
document.write("<br />")
document.write(encodeURIComponent(",/?:@&=+$#"))
</script>
输出:
http%3A%2F%2Fwww.w3school.com.cn
http%3A%2F%2Fwww.w3school.com.cn%2Fp%201%2F
%2C%2F%3F%3A%40%26%3D%2B%24%23
escape()
escape()
函数可把字符串作为 URI 组件进行编码。
<script type="text/javascript">
document.write(escape("Visit W3School!") + "<br />")
document.write(escape("?!=()#%&"))
</script>
输出:
Visit%20W3School%21
%3F%21%3D%28%29%23%25%26
eval()
eval()
函数可计算某个字符串,并执行其中的的 JavaScript 代码。
<script type="text/javascript">
eval("x=10;y=20;document.write(x*y)")
document.write(eval("2+2"))
var x=10
document.write(eval(x+17))
</script>
输出:
200
4
27
isNaN()
isNaN()
函数用于检查其参数是否是非数字值。
<script>
document.write(isNaN(123));
document.write(isNaN(-1.23));
document.write(isNaN(5-2));
document.write(isNaN(0));
document.write(isNaN("Hello"));
document.write(isNaN("2005/12/12"));
</script>
输出:
false
false
false
false
true
true
Number()
Number()
函数把对象的值转换为数字。
<script type="text/javascript">
var test1= new Boolean(true);
var test2= new Boolean(false);
var test3= new Date();
var test4= new String("999");
var test5= new String("999 888");
document.write(Number(test1)+ "<br />");
document.write(Number(test2)+ "<br />");
document.write(Number(test3)+ "<br />");
document.write(Number(test4)+ "<br />");
document.write(Number(test5)+ "<br />");
</script>
输出:
1
0
1256657776588
999
NaN
parseFloat()
parseFloat()
数可解析一个字符串,并返回一个浮点数。
<script type="text/javascript">
document.write(parseFloat("10"))
document.write(parseFloat("10.00"))
document.write(parseFloat("10.33"))
document.write(parseFloat("34 45 66"))
document.write(parseFloat(" 60 "))
document.write(parseFloat("40 years"))
document.write(parseFloat("He was 40"))
</script>
输出:
10
10
10.33
34
60
40
NaN
parseInt()
parseInt()
函数可解析一个字符串,并返回一个整数。
parseInt("10"); //返回 10
parseInt("19",10); //返回 19 (10+9)
parseInt("11",2); //返回 3 (2+1)
parseInt("17",8); //返回 15 (8+7)
parseInt("1f",16); //返回 31 (16+15)
parseInt("010"); //未定:返回 10 或 8
网友评论