1. 尝试访问未声名的变量会抛出错误,但是通过查询window对象,可以知道可能未声名的变量是否存在
<script>
console.log(value) // Uncaught ReferenceError: value is not defined
console.log(window.value) // undefined
</script>
2. location 对象
属性名 | 说明 | 例子 |
window.location.hash | 地址栏 #及以后的字符 | #aa |
window.location.href | 地址栏 | |
window.location.host | 域名以及端口号 | localhost:8080 |
window.location.port | 端口号 | 8080 |
window.location.hostname | 域名 | wise.36kr.com |
window.location.protocol | 页面使用协议 | https: |
window.location.search | 携带的参数 以?开头 | ?id=123&age=122 |
window.location.pathname | 目录或文件名称 | /wise-2020-newvane-qf |
3. replace()
url跳转,页面不可后退
var oButton = document.getElementById('btn')
oButton.onclick = function () {
window.location.replace('http://baidu.com')
}
4. navigator
最常使用的是 navigator.userAgent 来区分设备和浏览器
let userAgent = (window.navigator && window.navigator.userAgent) || ''
userAgent = userAgent.toLowerCase()
let isIPad = (/iPad/i).test(userAgent)
let isIPhone = (/iPhone/i).test(userAgent) && !isIPad
let isIPod = (/iPod/i).test(userAgent)
let isIOS = isIPhone || isIPad || isIPod
let isAndroid = (/Android/i).test(userAgent)
let isWechat = (/micromessenger/.test(userAgent))
5. history
history.go()
history.back()
history.forward()
history.length