首頁  >  文章  >  web前端  >  javascript學習筆記(十四) window物件使用介紹_基礎知識

javascript學習筆記(十四) window物件使用介紹_基礎知識

WBOY
WBOY原創
2016-05-16 17:52:331076瀏覽

1.視窗位置
以下取得瀏覽器視窗距螢幕左邊及上邊的位置

複製程式碼 程式碼如下:

var leftPos = (typeof window.screenLeft == "number") ? window.screenLeft : window.screenX; //左邊位置
var topPos = (typeof window.screenX; //左邊位置
var topPos = (typeof window.screenX; //左邊位置
var topPos = (typeof window.screenTop == "number" ) ? window.screenTop : window.screenY; //上邊位置


2.瀏覽器大小
以下取得瀏覽器頁面視窗的大小以下取得瀏覽器頁面視窗的大小以下取得瀏覽器頁面視窗的大小





複製程式碼


程式碼如下:


var pageWidth = window.innerWidth,
pageHeight = windowWidth = window.innerWidth,
pageHeight = window. if (typeof pageWidth != "number") {
if (document.compatMode == "CSS1Compat") {
pageWidth = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientWidth;
pageHeight = document.t. >} else { pageWith = document.body.clientWdith; pageHeight = document.body.clientHeight; } }
}
}


3.開啟或彈出視窗
window.open()方法,可以接受4個參數,通常只需指定第一個參數,第一個參數為URL,第二個參數為_self 、_parent 、_top 、_blank 或框架名



複製程式碼

程式碼如下: window.open("http://www.baidu.com" ); window.open("http://www.baidu.com","_blank"); window.open("http://www.baidu.com","topFrame"," height=400,width=400,top=10,left=10,resizable = yes"); topFrame.resizeTo(500,300); //調整視窗大小
topFrame.moveTo(100,100); //移動移動視窗位置
topFrame.close(); //關閉新開啟的視窗,IE會報錯


4.location 物件
location.href(URL) 載入URL




複製程式碼


複製程式碼

程式碼如下: location.href(URL) 載入URL http://www.baidu.com"); location.href = "http://www.baidu.com" ; //同上location.assign = "http://www.baidu. com"; //同上
window.loaction = "http://www.baidu.com"; //同上
location.replace("http://www.baidu.com"); //同上,但不能回退

location.reload(); //重新載入(可能從快取中載入)
location.reload(true); //重新載入(從伺服器載入)

location.search() 傳回URL中的查詢字串,字串以為?開頭


5.取得查詢字串參數




複製程式碼



複製程式碼


程式碼如下:

🎜>
function getQueryStringArgs() {
var qs = (location.search.length > 0) location.search.substring(1) : ""; var args ={}; var items = qs.split("&"); var item = null,name = null,value = null; for (var i=0 ; i{ item = itmes[i].split("=");
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]);
args[name] = decodeURIComponent(item[1]);
args[name] = value;
}
return args;
}

//假設查詢字串參數是?q=javascript&num=10
var args = getQueryStringArgs();
alert (args["q"]); //"javascript"
alert(args["num"]); //"10"

6.history 物件複製程式碼
程式碼如下:


history.go() 頁跳到
history.go(-1 ); //後退一頁
history.go(1); //前進一頁
history.go(2); //前兩頁
history.go("baidu.com") ; 跳到最近的baidu.com頁面
history.back(); //後退一頁history.forword(); //前進一頁 偵測目前頁是不是使用者開啟的第一頁
複製程式碼 程式碼如下: 程式碼如下: if (history.length == 0) { //如果開啟的是第一個頁面的話,執行某些操作} 7.頁面載入window .onload() 用於頁面載入結束後做某些操作複製程式碼 程式碼如下:

window.onload = function () {
//執行某些動作
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn