js中常用的browser和dom對象操作總結

來源:互聯網
上載者:User

標籤:

 

一、常用的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對象操作總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.