1 需求
2 语法
- Window 对象
- alert(message)
- prompt(msg,defaultText)
- confirm(message)
- window.open(URL,name,specs,replace)
- window.close()
- setInterval(code,millisec,lang)
- clearInterval(id_of_setinterval)
- setTimeout(code,millisec,lang)
- clearTimeout(id_of_settimeout)
Navigator 对象
- 对象属性
- navigator.platform
- navigator.userAgent
- navigator.appName
- navigator.appVersion
- navigator.appCodeName
- navigator.cookieEnabled
- 对象方法
- navigator.javaEnabled()
- navigator.taintEnabled()
- Screen 对象
- 对象属性
- screen.availHeight
- screen.availWidth
- screen.height
- screen.width
- screen.colorDepth
- screen.pixelDepth
- History 对象
- 对象属性
- history.length
- 对象方法
- history.back()
- history.forward()
- history.go(number|URL)
- Location 对象
- 对象属性
- location.href
- location.protocol
- location.host
- location.hostname
- location.port
- location.pathname
- location.search
- location.hash
- 对象方法
- location.assign(URL)
- location.reload(forceGet)
- location.replace(newURL)
3 示例
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8" />
- <title>JavaScript BOM Demo</title>
- <style>
- h1 {
- text-align: center;
- }
- </style>
- <script>
- mWindow;
-
- function openWindow() {
- mWindow = window.open("https://www.baidu.com");
- }
-
- function closeWindow() {
- mWindow.close();
- }
-
- function mBack() {
- window.history.back();
- }
-
- function mForward() {
- window.history.forward();
- }
-
- function mGo() {
- window.history.go(-1);
- }
- </script>
- </head>
- <body>
- <h1>Window对象 属性和方法 演示</h1>
- <button onclick="javascript:window.alert('xss');">alert</button>
- <button onclick="javascript:window.prompt('title','content');">prompt</button>
- <button onclick="javascript:window.confirm('确认退出?');">confirm</button><br>
- <button onclick="openWindow()">打开新窗口</button>
- <button onclick="closeWindow()">关闭指定窗口</button>
- <button onclick="javascript:window.close();">关闭当前窗口</button><br>
- <button onclick="javascript:setInterval(function(){alert('xss');},1000);">setInterval</button>
- <hr>
- <h1>Screen对象 属性和方法 演示</h1>
- <script>
- document.write('screen.availHeight: ' + screen.availHeight + '
'); - document.write('screen.availWidth: ' + screen.availWidth + '
'); - document.write('screen.height: ' + screen.height + '
'); - document.write('screen.width: ' + screen.width + '
'); - document.write('screen.colorDepth: ' + screen.colorDepth + '
'); - document.write('screen.pixelDepth: ' + screen.pixelDepth + '
'); - </script>
- <hr>
- <h1>Navigator对象 属性和方法</h1>
- <script>
- document.write('navigator.platform: ' + navigator.platform + '
'); - document.write('navigator.userAgent: ' + navigator.userAgent + '
'); - document.write('navigator.appName: ' + navigator.appName + '
'); - document.write('navigator.appCodeName: ' + navigator.appCodeName + '
'); - document.write('navigator.appVersion: ' + navigator.appVersion + '
'); - document.write('navigator.cookieEnabled: ' + navigator.cookieEnabled + '
'); - document.write('navigator.javaEnabled(): ' + navigator.javaEnabled() + '
'); - </script>
- <hr>
- <h1>Location对象 属性和方法 演示</h1>
- <script>
- document.write('location.href: ' + location.href + '
'); - document.write('location.protocol: ' + location.protocol + '
'); - document.write('location.host: ' + location.host + '
'); - document.write('location.hostname: ' + location.hostname + '
'); - document.write('location.port: ' + location.port + '
'); - document.write('location.pathname: ' + location.pathname + '
'); - document.write('location.search: ' + location.search + '
'); - document.write('location.hash: ' + location.hash + '
'); - </script>
- <button onclick="javascript:window.location.assign('https:\/\/www.baidu.com');">assign</button>
- <button onclick="javascript:window.location.replace('https\/\/www.baidu.com')">replace</button>
- <button onclick="location.reload()">reload</button>
- <hr>
- <h1>History对象 属性和方法 演示</h1>
- <script>
- document.write('history.length: ' + history.length + '
'); - </script>
- <button onclick="mBack()">back</button>
- <button onclick="mForward()">forward</button>
- <button onclick="mGo()">go</button>
- <hr>
- </body>
- </html>
-
- </!DOCTYPE>
4 参考资料