首页 / 知识
JavaScript的BOM
2023-04-11 15:09:00
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:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>Document</title>
<styletype="text/css">
.box1{
height:900px;
background:#ccc;
}
.box2{
height:500px;
background-color:#333;
}
</style>
</head>
<body>
<divid="box1"></div>
<div></div>
<inputtype="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:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>Document</title>
</head>
<body>
<inputtype="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:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>Document</title>
</head>
<body>
<ahref="example_2.html">example_2.html</a>
<inputtype="button"value="后退"id="btn1">
<inputtype="button"value="前进"id="btn2">
<script>
varbtn1=document.getElementById('btn1');
varbtn2=document.getElementById('btn2');
btn1.onclick=function(){
//history.back();
history.go(-1);
}
btn2.onclick=function(){
history.forward()
//history.go(1);
}
</script>
</body>
</html>
navigator.html:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
functiongetBrowser(){
varexplorer=navigator.userAgent.toLowerCase();
varbrowser="";
if(explorer.indexOf("msie")>-1){
browser="IE";
}elseif(explorer.indexOf("chrome")>-1){
browser='Chrome';
}else{
browser='asdf';
}
returnbrowser;
}
varmsg="您用的是"+getBrowser()+'浏览器';
console.log(msg);
</script>
</body>
</html>
screen.html:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="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>
本文转载自中文网 |
最新内容
相关内容
python如何读取列表中元素的位置?
python如何读取列表中元素的位置?,位置,数据,异常,培训,字符串,元素,索引,方法,示例,结果,python读取列表中元素位置的方法:1、使用index()方python怎么使用文件夹下的脚本?
python怎么使用文件夹下的脚本?,工作,培训,文件夹,脚本,文件,所在,方法,示例,路径,以上,python中使用文件夹下脚本的方法:将当前的工作目录(即python中获取路径的三种方法
python中获取路径的三种方法,工作,代码,情况,培训,下来,路径,文件,也就是,桌面,目录,python中获取路径总结下来分为三种情况:1、获取工作目录python如何调用另一个文件夹中的内
python如何调用另一个文件夹中的内容?,系统,培训,文件,模块,内容,路径,函数,所在,前缀,语句,python中调用另外一个文件夹中的内容:1、同一文件python怎么找出所有的数字?
python怎么找出所有的数字?,数字,培训,代码,小数点,小数,字符串,整数,表达式,含义,思路,python中获取字符串中所有数字的方法:1、使用正则表达Python怎么取出列表中的相邻元素?
Python怎么取出列表中的相邻元素?,代码,异常,培训,元素,指针,序列,对象,表示,语句,函数,1、python的迭代器。iter()能把一个序列生成为一个和Python慢的重要原因
Python慢的重要原因,信息,培训,语言,编译器,整数,操作,程序,解释性,速度,原因,1、python是动态性语言不是静态性语言在python程序执行的时候,如何在python代码中指定保存的文件
如何在python代码中指定保存的文件格式,代码,培训,文件格式,格式,二进制文件,文件,后缀,以上,方法,更多,python指定保存文件格式的方法:1、保Python如何复制文件中的内容
Python如何复制文件中的内容,盘中,数据,培训,文件,内容,方法,文件夹,路径,源文件,文件名,python复制文件中内容的方法:1、使用shutil.copyfile实数是不是python的数据类型?
实数是不是python的数据类型?,数字,标准,培训,实数,数据类型,数轴,复数,有限小数,无理数,虚数,实数是python的数据类型。实数,是有理数和无理linux下怎么查看python的包
linux下怎么查看python的包,软件,培训,工具,管理,命令,格式,详细信息,下面,以上,参数,linux中可以使用piplist命令查看安装的python包。pip是python中的open函数如何编码?
python中的open函数如何编码?,数据,系统,可取,培训,文件,参数,函数,时候,表示,模式,python中的open函数可以通过在打开文件时添加encoding参