057_全局对象
1. 在JavaScript代碼嵌入一個特殊環境中時, 全局對象通常具有環境特定的屬性。在瀏覽器中, JavaScript全局對象就是Window對象。
2. 全局對象只是一個對象, 而不是類。既沒有構造函數, 也無法實例化一個新的全局對象。
3. 全局函數
4. 全局屬性
5. encodeURI()函數
5.1. encodeURI()函數可把字符串作為URI進行編碼。
5.2. 語法
encodeURI(URIstring)5.3. 參數
5.4. 返回URIstring的副本, 其中的某些字符將被十六進制的轉義序列進行替換。
5.5. 該方法不會對ASCII字母和數字進行編碼, 也不會對這些ASCII標點符號進行編碼。
6. encodeURIComponent()函數
6.1. encodeURIComponent()函數可把字符串作為URI組件進行編碼。
6.2. 語法
encodeURIComponent(URIstring)6.3. 參數
6.4. 返回URIstring的副本, 其中的某些字符將被十六進制的轉義序列進行替換。
6.5. 該方法不會對ASCII字母和數字進行編碼, 也不會對這些ASCII標點符號進行編碼。不過, 這些用于分隔URI組件的標點符號(;/?:@&=+$,#), 會由一個或多個十六進制的轉義序列替換。
7. decodeURI()函數
7.1. decodeURI()函數可對encodeURI()函數編碼過的URI進行解碼。
7.2. 語法
decodeURI(URIstring)7.3. 參數
7.4. 返回URIstring的副本, 其中的十六進制轉義序列將被它們表示的字符替換。
8. decodeURIComponent()函數
8.1. decodeURIComponent()函數可對encodeURIComponent()函數編碼的URI進行解碼。
8.2. 語法
decodeURIComponent(URIstring)8.3. 參數
8.4. 返回URIstring的副本, 其中的十六進制轉義序列將被它們表示的字符替換。
9. eval()函數
9.1. eval()函數可計算某個字符串, 并執行其中的的JavaScript代碼。
9.2. 語法
eval(string)9.3. 參數
9.4. 返回通過計算string得到的值(如果有的話)。
9.5. 該方法只接受原始字符串作為參數, 如果string參數不是原始字符串, 那么該方法將不作任何改變地返回。因此請不要為eval()函數傳遞String對象來作為參數。
9.6. 雖然eval()的功能非常強大, 但在實際使用中用到它的情況并不多。而且eval()效率低下。
10. 例子
10.1. 代碼
<!DOCTYPE html> <html lang="zh-CN"><head><meta charset="utf-8" /><title>全局函數</title></head><body><script type="text/javascript">var userName = '張三@qq.com';var password = 'a !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~1';var uri = 'http://www.zrq.com/login.action?';var eURI = uri + 'userName=' + encodeURI(userName) + '&password=' + encodeURI(password);var eURICpt = uri + 'userName=' + encodeURIComponent(userName) + '&password=' + encodeURIComponent(password);document.write('userName = ' + userName + ', password = ' + password + '<hr />'); document.write(eURI + '<br />');document.write(eURICpt + '<hr />');document.write(decodeURI(eURI) + '<br />');document.write(decodeURIComponent(eURICpt) + '<hr />');eval("x=10;y=20;document.write(x+' * '+y+' = '+x*y);");</script></body> </html> <!-- 空格 ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ 空格 ! %22 # $ %25 & ' ( ) * + , - . / : ; %3C = %3E ? @ %5B %5C %5D %5E _ %60 %7B %7C %7D ~ 空格 ! %22 %23 %24 %25 %26 ' ( ) * %2B %2C - . %2F %3A %3B %3C %3D %3E %3F %40 %5B %5C %5D %5E _ %60 %7B %7C %7D ~" % < > [ \ ] ^ ` { | } 空格 # $ & + , / : ; = ? @ 奇怪的是encodeURIComponent為什么沒有對!'()~五個字符編碼 -->10.2. 效果圖
總結
- 上一篇: 061_JavaScript闭包
- 下一篇: 012_原始值和引用值