• WEB开发技能树-JavaScript-BOM-Window对象


    1 需求

    • Window对象
      • 需求1:警告框
      • 需求2:对话框
      • 需求3:确认框
      • 需求4:打开一个页面,可以实现跳转
      • 需求5:关闭页面
    • History对象
      • 需求1:后退一个历史页面
      • 需求2:前进一个历史页面
      • 需求3:跳转到指定历史页面


    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 示例

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>JavaScript BOM Demo</title>
    6. <style>
    7. h1 {
    8. text-align: center;
    9. }
    10. </style>
    11. <script>
    12. mWindow;
    13. function openWindow() {
    14. mWindow = window.open("https://www.baidu.com");
    15. }
    16. function closeWindow() {
    17. mWindow.close();
    18. }
    19. function mBack() {
    20. window.history.back();
    21. }
    22. function mForward() {
    23. window.history.forward();
    24. }
    25. function mGo() {
    26. window.history.go(-1);
    27. }
    28. </script>
    29. </head>
    30. <body>
    31. <h1>Window对象 属性和方法 演示</h1>
    32. <button onclick="javascript:window.alert('xss');">alert</button>
    33. <button onclick="javascript:window.prompt('title','content');">prompt</button>
    34. <button onclick="javascript:window.confirm('确认退出?');">confirm</button><br>
    35. <button onclick="openWindow()">打开新窗口</button>
    36. <button onclick="closeWindow()">关闭指定窗口</button>
    37. <button onclick="javascript:window.close();">关闭当前窗口</button><br>
    38. <button onclick="javascript:setInterval(function(){alert('xss');},1000);">setInterval</button>
    39. <hr>
    40. <h1>Screen对象 属性和方法 演示</h1>
    41. <script>
    42. document.write('screen.availHeight: ' + screen.availHeight + '
      '
      );
    43. document.write('screen.availWidth: ' + screen.availWidth + '
      '
      );
    44. document.write('screen.height: ' + screen.height + '
      '
      );
    45. document.write('screen.width: ' + screen.width + '
      '
      );
    46. document.write('screen.colorDepth: ' + screen.colorDepth + '
      '
      );
    47. document.write('screen.pixelDepth: ' + screen.pixelDepth + '
      '
      );
    48. </script>
    49. <hr>
    50. <h1>Navigator对象 属性和方法</h1>
    51. <script>
    52. document.write('navigator.platform: ' + navigator.platform + '
      '
      );
    53. document.write('navigator.userAgent: ' + navigator.userAgent + '
      '
      );
    54. document.write('navigator.appName: ' + navigator.appName + '
      '
      );
    55. document.write('navigator.appCodeName: ' + navigator.appCodeName + '
      '
      );
    56. document.write('navigator.appVersion: ' + navigator.appVersion + '
      '
      );
    57. document.write('navigator.cookieEnabled: ' + navigator.cookieEnabled + '
      '
      );
    58. document.write('navigator.javaEnabled(): ' + navigator.javaEnabled() + '
      '
      );
    59. </script>
    60. <hr>
    61. <h1>Location对象 属性和方法 演示</h1>
    62. <script>
    63. document.write('location.href: ' + location.href + '
      '
      );
    64. document.write('location.protocol: ' + location.protocol + '
      '
      );
    65. document.write('location.host: ' + location.host + '
      '
      );
    66. document.write('location.hostname: ' + location.hostname + '
      '
      );
    67. document.write('location.port: ' + location.port + '
      '
      );
    68. document.write('location.pathname: ' + location.pathname + '
      '
      );
    69. document.write('location.search: ' + location.search + '
      '
      );
    70. document.write('location.hash: ' + location.hash + '
      '
      );
    71. </script>
    72. <button onclick="javascript:window.location.assign('https:\/\/www.baidu.com');">assign</button>
    73. <button onclick="javascript:window.location.replace('https\/\/www.baidu.com')">replace</button>
    74. <button onclick="location.reload()">reload</button>
    75. <hr>
    76. <h1>History对象 属性和方法 演示</h1>
    77. <script>
    78. document.write('history.length: ' + history.length + '
      '
      );
    79. </script>
    80. <button onclick="mBack()">back</button>
    81. <button onclick="mForward()">forward</button>
    82. <button onclick="mGo()">go</button>
    83. <hr>
    84. </body>
    85. </html>
    86. </!DOCTYPE>


    4 参考资料

  • 相关阅读:
    阿里云国际版云服务器Windows系统数据恢复操作步骤
    php面向对象-抽象一个类
    MySQL中的 增 删 查 改(CRUD)
    JavaScript中 slice, substr 和 substring 的区别
    停止线程的方法
    java毕业设计诊所信息管理系统Mybatis+系统+数据库+调试部署
    2022年最火的十大测试工具,你掌握了几个
    02 C++ 变量和基本类型
    SSTI注入利用姿势合集
    leetcode187. 重复的DNA序列
  • 原文地址:https://blog.csdn.net/pwp032984/article/details/128074252