JavaScript的BOM
發表時間:2023-09-16 來源:明輝站整理相關軟件相關文章人氣:
[摘要]這次給大家帶來JavaScript的BOM,使用JavaScript的BOM的注意事項有哪些,下面就是實戰案例,一起來看一下。location對象location對象提供了與當前窗口中加載的文檔有關的信息,還提供了一些導航的功能,它既是window對象的屬性,也是document對象的屬性。語法:...
這次給大家帶來JavaScript的BOM,
使用JavaScript的BOM的注意事項有哪些,下面就是實戰案例,一起來看一下。
location對象
location對象提供了與當前窗口中加載的文檔有關的信息,還提供了一些
導航的功能,它既是window對象的屬性,也是document對象的屬性。
語法:location.href
功能:返回當前加載頁面的完整URL
說明:location.href與window.location.href等價
語法:location.hash
功能:返回URL中的hash(#號后跟零或多個字符),如果不包含則返回空字符串
語法:location.host
功能:返回服務器名稱和端口號(如果有)
語法:locationhostname
功能:返回不帶端口號的服務器名稱。
語法:location.pathname
功能:返回URL中的目錄和(或)文件名。
語法:location.port
功能:返回URL中指定的端口號,如果沒有,返回空字符串。
語法:location.protocol
功能:返回頁面使用的協議
語法:location.search
功能:返回URL的查詢字符串。這個字符串以問號開頭。
語法:location.replace(url)
功能:重新定向URL
說明:使用location.replace不會再歷時記錄中生成新紀錄。
語法:location.reload()
功能:重新加載當前顯示的頁面。
說明:
location.reload()有肯從緩沖中加載
location.reload(true)從服務器重新加載
history對象
history對象保存了用戶在瀏覽器中訪問頁面的歷史記錄
語法:history.back()
功能:回到歷史記錄的上一步
說明:相當于使用了history.go(-1)
語法:location.forward()
功能:回到歷時記錄的下一步
說明:相當于使用了history.go(1)
語法:history.go(-n)
功能:回到歷時記錄的前n步
語法:history.go(n)
功能:回到歷史記錄的后n步
navigator對象
useragent:用來識別瀏覽器名稱,版本,引擎以及操作系統等信息的內容。
screen對象
語法:screen.availWidth
功能:返回可用的屏幕寬度
語法:screen.availHeight
功能:返回可用的屏幕高度
location01.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.box1{
height: 900px;
background: #ccc;
}
.box2{
height: 500px;
background-color: #333;
}
</style>
</head>
<body>
<div id="box1"></div>
<div></div>
<input type="button" id="btn" value="返回頂部">
<script>
btn.onclick = function () {
location.hash = '#box1';
console.log(location.hash);
}
console.log(location.href);
console.log(location.hash);
console.log(location.host);
console.log(location.hostname);
console.log(location.pathname);
console.log(location.port);
console.log(location.protocol);
console.log(location.search);
</script>
</body>
</html>
location02.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="button" value="刷新" id="btn">
<script>
/*setTimeout(function () {
//location.href = "https://www.baidu.com";
//window.location = "https://www.baidu.com";
location.replace("https://www.baidu.com");
},1000);*/
document.getElementById('btn').onclick = function () {
location.reload();
//location.reload(true);
}
</script>
</body>
</html>
history01.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<a href="example_2.html">example_2.html</a>
<input type="button" value="后退" id="btn1">
<input type="button" value="前進" id="btn2">
<script>
var btn1 = document.getElementById('btn1');
var btn2 = document.getElementById('btn2');
btn1.onclick = function () {
//history.back();
history.go(-1);
}
btn2.onclick = function () {
history.forward()
//history.go(1);
}
</script>
</body>
</html>
navigator.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
function getBrowser() {
var explorer = navigator.userAgent.toLowerCase();
var browser = "";
if (explorer.indexOf("msie")>-1) {
browser = "IE";
} else if (explorer.indexOf("chrome")>-1){
browser = 'Chrome';
} else {
browser = 'asdf';
}
return browser;
}
var msg = "您用的是" +getBrowser()+'瀏覽器';
console.log(msg);
</script>
</body>
</html>
screen.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
console.log(screen.availWidth);
console.log(screen.availHeight);
console.log(window.innerWidth);
console.log(window.innerHeight);
</script>
</body>
</html>
相信看了這些案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
相關閱讀:
ES6 module語法加載 import export
判斷登陸是否失效代碼
如何利用getBoundingClientRect()來實現div容器滾動固定
實現瀑布流布局的倆種方法
以上就是JavaScript的BOM的詳細內容,更多請關注php中文網其它相關文章!
網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。