33-JavaScript-DOM-window对象常用方法2-属性

冷不防 2022-09-18 11:53 268阅读 0赞

- window常用方法2
-
- window属性

1. 方法

1) window.moveTo(x,y)

① 功能

可把窗口的左上角移动到一个指定的坐标。

② 示例(注: 仅IE通过)

  1. <body>
  2. <input type="button" value="moveTo()" οnclick="toNewPosition()"/>
  3. </body>
  4. <script type="text/javascript">
  5. function toNewPosition() {
  6. window.moveTo(300, 300);
  7. }
  8. </script>

2) window.moveBy(x,y)

① 功能

可相对窗口的当前坐标把它移动指定的像素。

② 示例(仿 QQ 窗口抖动, IE可通过)

  1. <body>
  2. <input type="button" value="start" οnclick="remind()"/>
  3. <input type="button" value="stop" οnclick="stop()"/>
  4. </body>
  5. <script type="text/javascript">
  6. var interval_id;
  7. // 仿 QQ 窗口抖动
  8. function remind() {
  9. interval_id = window.setInterval(move, 200);
  10. }
  11. function stop() {
  12. window.clearInterval(interval_id);
  13. }
  14. function move() {
  15. window.moveBy(20, 0);
  16. window.moveBy(0, 20);
  17. window.moveBy(-20, 0);
  18. window.moveBy(0, -20);
  19. }
  20. </script>

3) resizeTo()

resizeBy()

① 原窗口的 宽=200px 高=300px

resizeTo(100, 100)
现窗口的 宽=100px 高=100px

② 原窗口的 宽=200px 高=300px

resizeBy(100, 100)
现窗口的 宽=200px+100px 高=300px+100px

4) open(url[, name][, features][, replace])

/ close()

① 功能

打开一个新的浏览器窗口或查找一个已命名的窗口
返回 该窗口window对象的引用

② 示例

  1. <body>
  2. <input type="button" value="openNewWindow_1" οnclick="openNewWindow_1()"/>
  3. <input type="button" value="openNewWindow_2" οnclick="openNewWindow_2()"/>
  4. </body>
  5. <script type="text/javascript">
  6. function openNewWindow_1() {
  7. window.open("http://www.baidu.com", "_blank");
  8. // <==> window.open("http://www.baidu.com");
  9. }
  10. function openNewWindow_2() {
  11. window.open("http://www.baidu.com", "_blank",
  12. "height=500, width=600px, " + // 宽 高
  13. "directories=no, " + // 目录按钮
  14. "location=no, " + // 显示地址字段
  15. "menubar=no, " + // 显示菜单栏
  16. "resizable=no, " + // 可调节尺寸
  17. "titlebar=no"
  18. );
  19. }
  20. </script>

2. 属性

1) document

获取window的document对象 的引用.
注: window对象可以获得所有 BOM对象的引用.

2) opener

① 功能

返回对创建该窗口的 Window 对象的引用

② 示例-两页面传值

  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html;charset=utf-9" />
  4. <script type="text/javascript">
  5. function openNewWindow() {
  6. var newWindow = window.open("childWindow.html", "_blank");
  7. // 给新窗口中的文本框赋初值
  8. newWindow.onload = function() {
  9. this.document.getElementById("username").value = "123";
  10. }
  11. }
  12. </script>
  13. </head>
  14. <body>
  15. <input type="button" value="openNewWindow" οnclick="openNewWindow()"/>
  16. <div id="demoDiv">...</div>
  17. </body>
  18. </html>

childWindow.html

  1. <script type="text/javascript">
  2. var parentWindow = window.opener;
  3. var parentWindowDocument = parentWindow.document;
  4. var parentWindowDiv = parentWindowDocument.getElementById("demoDiv");
  5. // 将username放入 父窗口中的div
  6. function notify() {
  7. var username = document.getElementById("username").value;
  8. // parentWindowDiv.innerText = username; // FireFox不支持 innerText
  9. parentWindowDiv.innerHTML = username;
  10. }
  11. </script>
  12. <input type="text" id="username">
  13. <input type="button" value="submit" οnclick="notify()">

3. 示例

1) 登陆成功5秒后跳转到主页面

login.html -> success.html -> manage.html

  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  4. <script type="text/javascript">
  5. function $(id) {
  6. return document.getElementById(id);
  7. }
  8. function validateInfo() {
  9. var username = $("username").value;
  10. var password = $("password").value;
  11. if (username == "admin" && password == "123") {
  12. return true;
  13. }
  14. return false;
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <form action="success.html">
  20. usrename: <input type="text" id="username"><br/>
  21. password: <input type="text" id="password"><br/>
  22. <input type="submit" value="submit" οnclick="return validateInfo()">
  23. </form>
  24. </body>
  25. </html>
  26. <html>
  27. <head>
  28. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  29. <script type="text/javascript">
  30. var msg;
  31. var count = 5;
  32. window.onload = function() {
  33. msg = $("msg");
  34. window.setInterval(timeCount, 1000);
  35. }
  36. function $(id) {
  37. return document.getElementById(id);
  38. }
  39. function timeCount() {
  40. msg.innerHTML = --count;
  41. if (0 == count) {
  42. window.open("manage.html", "_self");
  43. }
  44. }
  45. </script>
  46. </head>
  47. <body>
  48. success!!!!!!!! <span id="msg">5</span> 秒后跳转的管理页面
  49. </body>
  50. </html>
  51. <body>
  52. <h3>manage page````````````````</h3>
  53. </body>

发表评论

表情:
评论列表 (有 0 条评论,268人围观)

还没有评论,来说两句吧...

相关阅读