window對象的方法屬性
方法
1、window.open(URL, name, ‘width=100,height=100,left=0,top=0’)
打開一個新窗口,可指定地址(URL),指定新窗口title(name,我試了沒反應啊),
此外還可以設置新窗口打開的位置,大小等。left、top、width、height(當未設置寬高時left、top谷歌不起作用,ie8以上設置寬或高即可)
2、window.close()
關閉本窗口
3、window.history.back()
回到上一頁
屬性
1、window.location屬性
示例:http://xxx.com:8080/index.html?id=1&name=周傑倫#abc
屬性 | 含義 | 值 |
href: | 獲取當前完整的URL值 | "http://xxx.com:8080/index.html?id=1&name=周傑倫#abc" |
protocol: | 獲取協議 | "http:" |
hostname: | 服務器名字 | "xxx.com" |
port: | 端口 | "8080" |
pathname: | URL端口後的部分 | "index.html" |
search: | ?後的查詢字符串 | "id=1&name=周傑倫" |
hash: | #之後的內容 | "abc" |
host: | hostname + port | "xxx.com:8080" |
- 在使用window.location.xx屬性時,可簡寫為location.xx
- window.location 等價於document.location
- 但是document.open() 與 window.open()不一樣,一個是打開新窗口,一個是在此document內。
在獲取location.search時,往往需要獲取參數值
function getPara(href, name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = href.match(reg); //獲取url中"?"符後的字符串並正則匹配
var context = "";
if (r != null)
context = r[2];
reg = null;
r = null;
return context == null || context == "" || context == "undefined" ? "" : context;
}
href為去掉?的字符串。name為你想獲取的參數
在頁面傳值獲取參數值時...又會遇到有些中文會經過url的轉碼,導致的亂碼
此時,我使用base64.js進行轉碼與解碼
window對象的方法屬性