window对象
- 在浏览器中,
window对象有双重角色,它既是通过Js访问浏览器窗口的一个接口,又是ECMAScript规定的Global对象
var age = 29;
function sayAge(){
alert(this.age);
}
alert(window.age); //29
sayAge(); //29
window.sayAge();
全局作用域定义的变量和函数会自动映射到window对象下
- 间隔调用和超时调用
指定时间后执行代码------->格式:setTimeout(func,time)
setTimeout(function(){
console.log("Hello world")
},1000)
//1秒后打印一个"hello world"
js是单线程语言,上述案例会在1秒后将要执行的函数放入任务队列,如果任务队列为空,则会立即执行,否则需要等待
清除定时任务:
var id=setTimeout(function(){
console.log("Hello world")
},1000);
clearTimeout(id);
每 n秒后执行一次------>格式:setInterval(func,time),其中time的单位为毫秒
setInterval(function(){
console.log("hello world")
},1000);
清除间隔调用任务:
var num=0;
var max=10;
var intervalId=null;
function incrementNumber(){
num++;
if(num==max){
clearInterval(intervalId);
}
}
intervalId=setInterval(incrementNumber,500);
location对象
location对象既是window对象的属性,也是document对象的属性,也就是说window.location和document.location引用的是同一个对象。location的常见属性:
- 对URL
?后面的参数解析
function getQueryStringArgs(){
//取得查询字符串并去掉开头的问号
var qs=(window.location.search.length>0)?(window.location.search.substring(1)):"";
//保存数据的对象
args={};
items=qs.length?qs.split("&"):[];
items.forEach(item => {
var detail=item.split("=");
//对url进行解析
var key= decodeURIComponent(detail[0]);
var value=decodeURIComponent(detail[1]);
args[key]=value;
});
return args;
}
console.log(getQueryStringArgs());
- 位置操作:
window.location,location.herf实际上最终都是调用location.assgin实现跳转的。
window.location="http://www.baidu.com";
location.href="http://www.baidu.com";
location.assign("http://www.baidu.com");
- 修改
location对象的其他属性也可以改变当前加载的页面(使页面重新加载):
//假设初始 URL 为 http://www.wrox.com/WileyCDA/
//将 URL 修改为"http://www.wrox.com/WileyCDA/#section1"
//hash不会使页面重新加载
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;
重新加载当前页面:location.reload():------>无参则从缓存中加载,有参则从服务器中重新加载。
//从缓存中重新加载
location.reload();
//从服务器中重新加载
location.reload(true);
示例用replace跳转--->1秒后跳转到某一个界面,但此时不能通过后退返回上一个页面,即不会在浏览器历史中产生记录
<!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.baidu.com")
},1000);
</script>
</body>
</html>
navigator对象:识别客户端浏览器的事实标准
- 检测插件:对于非
IE浏览器,可以使用plugins数组来判断 ---->该数组的每一项都包含以下属性:
name:插件名字,description:插件的描述,filename:插件的文件名,length:插件所要处理的MIME类型数量
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"))
- 在
IE中,要想检测特定的插件,就需要知道插件的COM标识符,并尝试通过ActiveXObject类型创建实例,若创建失败,则没有该插件------>如Flash的标识符是ShockwaveFlash.ShockwaveFlash。
function hasEPlugin(name) {
try {
var plugin = new ActiveXObject(name)
return true;
} catch (e) {
return false;
}
}
//检测Flash插件
alert(hasEPlugin(("QuickTime.QuickTime")));
- 检测
所有浏览器中的插件
//非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;
}
}
//IE
function hasEPlugin(name) {
try {
var plugin = new ActiveXObject(name)
return true;
} catch (e) {
return false;
}
}
//检测所有
function hasPluginForAllBrower(name){
var flag=hasPlugin(name);
if(flag){
return flag;
}else{
return hasEPlugin(name);
}
}
//检测Flash插件
alert(hasPluginForAllBrower(("QuickTime.QuickTime")));
screen对象
history对象
- 浏览器有多少个历史记录:
history.length 前进一页或者返回上一页:forward,back
//前进一页
history.forward();
//后退一页
history.back();
- 前进、后退
一页或者多页
//后退一页
history.go(-1);
//前进一页
history.go(1);
//前进2页
history.go(2);
- 当
go传入字符串时,此时浏览器会跳转到历史记录中包含该字符串的第一个位置-------->可能后退,也可能前进,具体要看哪个位置最近。
//跳转到最近的 wrox.com 页面
history.go("wrox.com");