標籤:
一、常用的Window對象操作
Window對象中又包含了document、history、location、Navigator和screen幾個對象,每個對象又有自己的屬性方法,這裡window可以省略。
如window.location.href 可以簡寫為location.href
//返回運行瀏覽器的作業系統和(或)硬體平台
var platform = navigator.platform;
//瀏覽器的代碼名
var appCodeName = navigator.appCodeName;
//瀏覽器的名稱
var appName = navigator.appName;
//瀏覽器的平台和版本資訊
var appVersion = navigator.appVersion;
//alert("platform:"+platform+"\n appCodeName:"+appCodeName+"\n appName:"+appName+"\n appVersion:"+appVersion);
//返回歷史列表中的網址數
var length = history.length;
//載入 history 列表中的下一個 URL
history.forward();
//載入 history 列表中的前一個 URL
history.back();
//載入曆史列表中的某個具體的頁面 -1上一個頁面,1前進一個頁面
history.go(-1);
//替換當前文檔 可以是當前項目根目錄下的文檔, 或者外部 網址
location.replace("index.html");
location.replace("http://www.baidu.com");
//返回 URL的錨部分 #開始的地方
var hash = location.hash;
//返回一個URL的查詢部分 ?之後的部分
var search = location.search;
//返回完整的URL(當前頁):
var href = location.href;
//重新整理當前文檔
location.reload();
//返回螢幕的總寬度和總高度
var width = window.screen.width;
var height = window.screen.height;
//在指定的毫秒數後執行
setTimeout(function(){
//TODO
},30000);
二、常用的HTML DOM操作
//通過getElementById擷取input輸入的值
var username = document.getElementById("username").value;
//通過querySelector擷取input輸入的值
var pwd = document.querySelector("#password").value;
//在id為txt的地方添加html
document.getElementById("txt").innerHTML="<h1>Hello World!</h1>";
//返回當前完整的url
var url = document.URL;
//返回當前文檔的標題
var title = document.title;
//返回當前文檔的網域名稱
var domain = document.domain;
//向輸出資料流寫入文本
document.write("Hello World!");
//向輸出資料流寫入格式文本
document.write("<h1>Hello World!</h1>");
js中常用的browser和dom對象操作總結