1)navigator.userAgent
浏览器代理检测,可以检测出来用户使用的浏览器类型,也可以检测浏览器所在的操作系统
navigator.userAgent
2)判断当前浏览器的名称
indexOf 代理检测 能力检测
var str = navigator.userAgent;
if(str.indexOf("Chrome")!=-1){
alert("谷歌浏览器");
}
3)判断PC端还是移动端
var arr = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
var str = navigator.userAgent;
for(var i = 0; i < arr.length; i++){
if(str.indexOf(arr[i])!=-1){
console.log("移动端");
location.href = "mobile.html";
}
}
4)location 对象
4)location 对象
可读可写 能取值能赋值
属性:hash、host、hostname、href、pathname、port、protocol、search
href:设置或返回完整的 URL。(常用)
hash:设置或返回从井号 (#) 开始的 URL(锚)。
search:设置或返回从问号 (?) 开始的 URL(查询部分)。(常用)
host 设置或返回主机名和当前 URL 的端口号。
hostname 设置或返回当前 URL 的主机名。
pathname 设置或返回当前 URL 的路径部分。
port 设置或返回当前 URL 的端口号。
protocol 设置或返回当前 URL 的协议。
方法:assign(url), replace(url) , reload()
assign() 加载新的文档。
reload() 重新加载当前文档。
replace() 用新的文档替换当前文档。
5)history对象 保存历史记录
- 后退一页 history.go(-1) history.back()
- 前进一页 history.go(1) history.forward()
- 前进两页 history.go(2)
- 5)window.onload 页面加载完毕后执