第八章 BOM(js高级程序设计)

2019-03-07  本文已影响0人  简默丶XS

window对象

var newValue = oldValue;//error
var newValue = window.oldValue;//这里不会抛出错误 newValue 的值是 undefined
  1. window.innerWidth
  2. window.innerHeight
  3. document.documentElement.clientWidth
  4. document.documentElement.clientHeight
    兼容性写法
width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight

书中demo是为了兼容ie6的混合模式:

  var pageWidth = window.innerWidth,
    pageHeight = window.innerHeight;
  if (typeof pageWidth != "number") {
    if (document.compatMode == "CSS1Compat") {
      pageWidth = document.documentElement.clientWidth;
      pageHeight = document.documentElement.clientHeight;
    } else {
      pageWidth = document.body.clientWidth;
      pageHeight = document.body.clientHeight;
    }
  }
    console.log(pageWidth)
    console.log(pageHeight)
var wroxWin = window.open("http://www.wrox.com/","wroxWindow","height=400,width=400,top=10,left=10,resizable=yes");
wroxWin.resizeTo(100,100)
wroxWin.moveTo(100,100)
wroxWin.close()
  var wroxWin = window.open("http://www.wrox.com", "_blank");
  if(wroxWin === null){
    alert('弹窗被浏览器拦截,请在关闭拦截后,重新加载页面!')
  }
  if (confirm('继续操作')) {
    alert('用户继续了操作')
  } else {
    alert('用户取消了操作')
  }
confirm
  1. 需要用户输入对话框prompt(),返回值null或者用户输入内容
  var result = prompt("Are you ok? ", "");
  if (result !== null) {
    alert("ok" + result);
  } else {
    alert('滚')
  }
  1. //显示“打印”对话框 window.print();
  2. //显示“查找”对话框 window.find();

location 对象

location对象属性
  function getQueryStringArgs() {
    //取得查询字符串并去掉开头的问号
    var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
      //保存数据的对象
      args = {},
      //取得每一项
      items = qs.length ? qs.split("&") : [],
      item = null,
      name = null, value = null,
      //在 for 循环中使用
      i = 0,
      len = items.length;
    //逐个将每一项添加到 args 对象中
    for (i = 0; i < len; i++) {
      item = items[i].split("=");
      name = decodeURIComponent(item[0]);
      value = decodeURIComponent(item[1]);
      if (name.length) {
        args[name] = value;
      }
    }
    return args;
  }
  1. location.assign("http://www.wrox.com");
  2. window.location = "http://www.wrox.com";
  3. location.href = "http://www.wrox.com"; //常用,其实就是调用了assign()
  4. location.replace("http://www.wrox.com/");//不会在history中新增一条访问记录,而是替换当前页面的history
  5. location.reload(); //重新加载(有可能从缓存中加载)
    location.reload(true); //重新加载(从服务器重新加载)//在版本更新时有用!

navigator 对象

screen 对象

screen 对象基本上只用来表明客户端的能力,其中包括浏览器窗口外部的显示器的信息,如像素宽度和高度等。每个浏览器中的 screen 对象都包含着各不相同的属性

  const win = window.open('www.baidu.com', '', 'a')
  win.resizeTo(screen.availWidth, screen.availHeight);

以弹窗的形式打开窗口win,并调整win为最大宽高(有些浏览器会禁用resizeTo,可能无效)

history对象

history 对象保存着用户上网的历史记录,从窗口被打开的那一刻算起。因为 history 是 window
对象的属性,因此每个浏览器窗口、每个标签页乃至每个框架,都有自己的 history 对象与特定的
window 对象关联。出于安全方面的考虑,开发人员无法得知用户浏览过的 URL。不过,借由用户访问过的页面列表,同样可以在不知道实际 URL 的情况下实现后退和前进。

用户是否第一次打开页面:

  if (history.length == 1) {
    alert('这应该是用户打开窗口后的第一个页面')
  }

书中length==0?为什么我看的是1

上一篇下一篇

猜你喜欢

热点阅读