javascript的window操作

來源:互聯網
上載者:User

標籤:https   res   啟動   編寫   browser   sign   for   實現   substring   

注意:此文檔是對於w3school的學習與整理

BOM瀏覽器物件模型(Browser Object Model)

1. 擷取window的尺寸

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

2.  window.open() - 開啟新視窗, 

3. window.close() - 關閉當前視窗,  

4. window.moveTo() - 移動當前視窗, 

5. window.resizeTo() - 調整當前視窗的尺寸

6. window.screen 對象包含有關使用者螢幕的資訊。

<script>document.write("可用寬度:" + screen.availWidth);document.write("可用高度:" + screen.availHeight);</script>

7. window.location 對象在編寫時可不使用 window 這個首碼。

  location.hostname 返回 web 主機的網域名稱
  location.pathname 返回當前頁面的路徑和檔案名稱
  location.port 返回 web 主機的連接埠 (80 或 443)
  location.protocol 返回所使用的 web 協議(http:// 或 https://)
  location.href 屬性返回當前頁面的 URL
  location.assign(url) 方法載入新的文檔

8. window.history 對象在編寫時可不使用 window 這個首碼。

  history.back() - 與在瀏覽器點擊後退按鈕相同
  history.forward() - 與在瀏覽器中點擊按鈕向前相同

9. window.navigator 對象包含有關訪問者瀏覽器的資訊。window.navigator 對象在編寫時可不使用 window 這個首碼。navigator 資料可被瀏覽器使用者更改.由於只有 Opera 支援屬性 "window.opera",您可以據此識別出 Opera。

<script>txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";txt+= "<p>Browser Name: " + navigator.appName + "</p>";txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";txt+= "<p>Platform: " + navigator.platform + "</p>";txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";document.getElementById("example").innerHTML=txt;</script>

10. 可以在 JavaScript 中建立三種訊息框:警告框、確認框、提示框。

  警告框: alert("文本")
  確認框: confirm("文本"); 如果使用者點擊確認,那麼傳回值為 true。如果使用者點擊取消,那麼傳回值為 false。
  提示框: prompt("文本","預設值"); 如果使用者點擊確認,那麼傳回值為輸入的值。如果使用者點擊取消,那麼傳回值為 null。

11. js實現計時

開始, 關閉功能

<html><head><script type="text/javascript">var c = 0;var t;function timedCount(){  document.getElementById(‘txt‘).value = c;  c = c + 1;  t = setTimeout("timedCount()", 1000);}function stopCount(){  c = 0;  setTimeout("document.getElementById(‘txt‘).value=0", 0);  clearTimeout(t);}</script></head><body><form>  <input type="button" value="開始計時!" onClick="timedCount()">  <input type="text" id="txt">  <input type="button" value="停止計時!" onClick="stopCount()"></form><p>請點擊上面的“開始計時”按鈕來啟動計時器。輸入框會一直進行計時,從 0 開始。點擊“停止計時”按鈕可以終止計時,並將計數重設為 0。</p></body></html>

即時計時

<html><head><script type="text/javascript">function startTime(){  var today=new Date();  var h=today.getHours();  var m=today.getMinutes();  var s=today.getSeconds();  // add a zero in front of numbers<10  m=checkTime(m);  s=checkTime(s);  document.getElementById(‘txt‘).innerHTML=h+":"+m+":"+s;  t=setTimeout(‘startTime()‘, 500);}function checkTime(i){  if (i < 10) {    i = "0" + i;  }  return i;}</script></head><body onload="startTime()"><div id="txt"></div></body></html>

12. 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)    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)  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>

  

  

  

  

  

javascript的window操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.