js(javaScript)學習系列--window

還是那句話,可以去看w3school

獲得window窗口大小

該例顯示瀏覽器窗口的高度和寬度:(不包括工具欄/滾動條
實用的 JavaScript 方案(涵蓋所有瀏覽器):

var w=window.innerWidth<!--Internet Explorer、Chrome、Firefox、Opera 以及 Safari-->
|| document.documentElement.clientWidth<!--Internet Explorer 8765-->
|| document.body.clientWidth;<!--Internet Explorer 8、7、6、5-->

var h=window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;

其他方法

  • window.open() - 打開新窗口
  • window.close() - 關閉當前窗口
  • dow.moveTo() - 移動當前窗口
  • ndow.resizeTo() - 調整當前窗口的尺寸

screen

window.screen 對象在編寫時可以不使用 window 這個前綴。
一些屬性:訪問者屏幕的寬度,以像素計,減去界面特性,比如窗口任務欄
screen.availWidth - 可用的屏幕寬度
screen.availHeight - 可用的屏幕高度

location

window.location 對象用於獲得當前頁面的地址 (URL),並把瀏覽器重定向到新的頁面。window.location 對象在編寫時可不使用 window 這個前綴。
location.hostname 返回 web 主機的域名
location.pathname 返回當前頁面的路徑和文件名
location.port 返回 web 主機的端口 (80 或 443)
location.protocol 返回所使用的 web 協議(http:// 或 https://)
location.href 屬性返回當前頁面的 URL。(http://www.w3school.com.cn/js/js_window_location.asp
location.pathname 屬性返回 URL 的路徑名。(/js/js_window_location.asp)
Location Assign Location.assign() 方法加載新的文檔。

<!DOCTYPE html>
<html>
<head>
<script>
function newDoc()
 {
 window.location.assign("http://www.w3school.com.cn")
 }
</script>
</head>
<body>
<input type="button" value="加載新文檔" onclick="newDoc()">
</body>
</html>

history

爲了保護用戶隱私,對 JavaScript 訪問該對象的方法做出了限制。
history.back() - 與在瀏覽器點擊後退按鈕相同
history.forward() - 與在瀏覽器中點擊按鈕向前相同

navigator 對象包含有關訪問者瀏覽器的信息
來自 navigator 對象的信息具有誤導性,不應該被用於檢測瀏覽器版本,這是因爲:
navigator 數據可被瀏覽器使用者更改
瀏覽器無法報告晚於瀏覽器發佈的新操作系統

由於 navigator 可誤導瀏覽器檢測,使用對象檢測可用來嗅探不同的瀏覽器。
由於不同的瀏覽器支持不同的對象,您可以使用對象來檢測瀏覽器。例如,由於只有 Opera 支持屬性 “window.opera”,您可以據此識別出 Opera。
例子:if (window.opera) {…some action…}

cookies

cookie 是存儲於訪問者的計算機中的變量。每當同一臺計算機通過瀏覽器請求某個頁面時,就會發送這個 cookie。你可以使用 JavaScript 來創建和取回 cookie 的值。

<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
{ 
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{ 
c_start=c_start + c_name.length+1 
c_end=document.cookie.indexOf(";",c_start)
<!--unescape 對字符串進行解碼-->
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
} 
}
return ""
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
<!--escape 對字符串進行編碼-->
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
}

function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
  {alert('Welcome again '+username+'!')}
else 
  {
  username=prompt('Please enter your name:',"")
  if (username!=null && username!="")
    {
    setCookie('username',username,365)
    }
  }
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章