JS的全局函數
全局屬性和函數可用於所有內建的 JavaScript 對象

關於編碼和解碼的一組方法:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>全局函數</title> <script> var str = "張三"; //alert(encodeURI(str));//%E5%BC%A0%E4%B8%89 //alert(encodeURIComponent(str));//%E5%BC%A0%E4%B8%89 //alert(decodeURI(encodeURI(str)));//張三 //alert(decodeURIComponent(encodeURIComponent(str)));//張三 var str1 = "http://www.itheima.cn"; //alert(encodeURI(str1));//http://www.itheima.cn //alert(encodeURIComponent(str1));//http%3A%2F%2Fwww.itheima.cn //alert(decodeURI(encodeURI(str1)));//http://www.itheima.cn //alert(decodeURIComponent(encodeURIComponent(str1)));//http://www.itheima.cn var str2 = "alert('abc')"; //alert(str2); eval(str2); </script> </head> <body> </body> </html>
在本例中,我們將使用 parseInt() 來解析不同的字符串:
parseInt("10"); //返回 10 默認十進制
parseInt("19",10); //返回 19 (10+9) 第二個參數10,表示十進制
parseInt("11",2); //返回 3 (2+1) 第二個參數2,表示二進制
parseInt("17",8); //返回 15 (8+7) 第二個參數8,表示八進制
parseInt("1f",16); //返回 31 (16+15)
parseInt("010"); //未定:返回 10 或 8