《Javascript 高级程序设计(第三版)》笔记0xD BOM location、navigator、screen、history

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/funkstill/article/details/84985889

目录

location 对象

     查询字符串参数

    位置操作

         replace()

        reload()

     检测插件

    注册处理程序

        registerContentHandler()

         registerProtocolHandler()

screen 对象

history 对象


location 对象

        location 对象是很特别的一个对象,既是 window 对象的属性,也是document 对象的属性;或者说说window.location 和 document.location 引用的是同一个对象。location 对象的用处不只表现在它保存着当前文档的信息,还表现在它将 URL 解析为独立的片段,让开发人员可以通过不同的属性访问这些片段。

location 对象的所有属性

     查询字符串参数

//解析查询字符串,返回包含所有参数的一个对象
function getQueryStringArgs(){
	//取得查询字符串并去掉开头的问号
	var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
	//保存数据的对象
	args = {},
	//取得每一项
	items = qs.length ? qs.split("&") : [],
	item = null,
	name = null,
	value = null,
	//在 for 循环中使用
	i = 0,
	len = items.length;
	//逐个将每一项添加到 args 对象中
	for (i=0; i < len; i++){
		item = items[i].split("=");
		name = decodeURIComponent(item[0]);
		value = decodeURIComponent(item[1]);
		if (name.length) {
			args[name] = value;
		}
	}
	return args;
}

//假设查询字符串是?q=javascript&num=10
var args = getQueryStringArgs();
alert(args["q"]); //"javascript"
alert(args["num"]); //"10"

    位置操作

        每次修改 location 的属性(hash 除外),页面都会以新 URL 重新加载

//打开新 URL 并在浏览器的历史记录中生成一条记录
location.assign("http://www.wrox.com");

//相当于
window.location = "http://www.wrox.com";
location.href = "http://www.wrox.com";


//假设初始 URL 为 http://www.wrox.com/WileyCDA/
//将 URL 修改为"http://www.wrox.com/WileyCDA/#section1"
location.hash = "#section1";
//将 URL 修改为"http://www.wrox.com/WileyCDA/?q=javascript"
location.search = "?q=javascript";
//将 URL 修改为"http://www.yahoo.com/WileyCDA/"
location.hostname = "www.yahoo.com";
//将 URL 修改为"http://www.yahoo.com/mydir/"
location.pathname = "mydir";
//将 URL 修改为"http://www.yahoo.com:8080/WileyCDA/"
location.port = 8080;

         replace()

<!--不会在历史记录中生成新记录。在调用 replace()方法之后,用户不能回到前一个页面-->
<!DOCTYPE html>
<html>
	<head>
		<title>You won't be able to get back here</title>
	</head>
	<body>
		<p>Enjoy this page for a second, because you won't be coming back here.</p>
		<script type="text/javascript">
			setTimeout(function () {
			location.replace("http://www.wrox.com/");
			}, 1000);
		</script>
	</body>
</html>

        reload()

//重新加载当前显示的页面。如果调用 reload()时不传递任何参数,页面就会以最有效的方式重新加载。
location.reload(); //重新加载(有可能从缓存中加载)
location.reload(true); //重新加载(从服务器重新加载)

navigator 对象

navigator存在于所有浏览器中的属性和方法

     检测插件

//检测插件(在 IE 中无效)
function hasPlugin(name){
	name = name.toLowerCase();
	for (var i=0; i < navigator.plugins.length; i++){
		if (navigator. plugins [i].name.toLowerCase().indexOf(name) > -1){
			return true;
		}
	}
	return false;
}
//检测 Flash
alert(hasPlugin("Flash"));
//检测 QuickTime
alert(hasPlugin("QuickTime"));

    注册处理程序

        registerContentHandler()

//将一个站点注册为处理 RSS 源的处理程序
navigator.registerContentHandler("application/rss+xml",
	"http://www.somereader.com?feed=%s", "Some Reader");

         registerProtocolHandler()

//注册一个 mailto 协议的处理程序,该程序指向一个基于 Web 的电子邮件客户端
navigator.registerProtocolHandler("mailto",
    "http://www.somemailclient.com?cmd=%s", "Some Mail Client");

screen 对象

history 对象

//后退一页
history.go(-1);
//前进一页
history.go(1);
//前进两页
history.go(2);

//跳转到最近的 wrox.com 页面
history.go("wrox.com");
//跳转到最近的 nczonline.net 页面
history.go("nczonline.net");

//后退一页
history.back();
//前进一页
history.forward();

if (history.length == 0){
//这应该是用户打开窗口后的第一个页面
}

猜你喜欢

转载自blog.csdn.net/funkstill/article/details/84985889
今日推荐