前端攻城狮让前端飞程序员

js浏览器相关

2018-05-17  本文已影响32人  tiancai啊呆

缓存


同源策略

跨域手段

只要跨域,父窗口与子窗口之间就无法获取对方的DOM,无法通信。为了解决这个问题,有如下方法:

document.domain,location.hash,postMessage是为了解决限制2和限制3的


重流与重绘

浏览器存储

拦截窗口的判断

主要利用window.open方法,该方法用于新建另一个浏览器窗口,并且返回该窗口对象。如果返回null则打开失败。

var newPop = window.open(url)  
if(!newPop){
  alert('您有默认程序阻止打开新窗口,请关闭后再试')
}

多窗口操作

window.top: 顶层窗口; window.parent: 父窗口;window.self: 当前窗口。
浏览器还提供一些特殊的窗口名,供open方法、<a>标签、<form>标签等引用。
_top:顶层窗口;_parent:父窗口;_blank:新窗口
下面代码就表示在顶层窗口打开链接。

<a href="somepage.html" target="_top">Link</a>

navigator对象

window对象的navigator属性,指向一个包含浏览器信息的对象。

navigator.userAgent        //返回浏览器的厂商和版本信息,用来识别浏览器
navigator.platform         //返回用户的操作系统信息。
navigator.plugins             //返回一个类似数组的对象,成员是浏览器安装的插件。
navigator.language       //使用的语言,用作国际化。
navigator.geolocation    //返回一个Geolocation对象,包含用户地理位置的信息。

Geolocation对象

getCurrentPosition方法,用来获取用户的地理位置。
watchPosition方法可以用来监听用户位置的持续改变,使用方法与getCurrentPosition方法一样。
watchPosition方法返回的标识符,用于供clearWatch方法取消监听。

    var geolocation = navigator.geolocation
    function geoSuccess(event) {
        console.log(event.timestamp)      //返回获得位置信息的具体时间
        console.log(event.coords)           //一个对象,包含了用户的位置信息
        console.log(event.coords.latitude)    //纬度
        console.log(event.coords.longitude)  //经度
        console.log(event.coords.accuracy)   //精度
        console.log(event.coords.altitude)    //海拔
        console.log(event.coords.altitudeAccuracy)   //海拔精度
        console.log(event.coords.heading)    //以360度表示的方向
        console.log(event.coords.speed)    //每秒的速度
    }
    function geoError(event) {
        console.log(event.code)     //表示错误类型
        console.log(event.message)   //表示错误信息
    }
    geolocation.getCurrentPosition(geoSuccess, geoError)

history对象

CDN

输入URL发生了什么

  1. DNS域名解析,得到IP地址。
  2. 向该IP对应的服务器发起请求。
  3. 服务器处理请求,返回一个响应。
  4. 浏览器得到响应,开始渲染。

DNS域名解析过程

  1. 浏览器会先查找有没有缓存的DNS记录,有直接返回,没有的话走2;
  2. 查找操作系统缓存的记录,有直接返回,没有的话走3;
  3. 查找路由器的记录,有直接返回,没有的话走4;
  4. 查找ISP的DNS缓存记录,有直接返回,没有的话走5;
  5. 从根域名服务器开始递归(或者迭代)搜索最后得到IP地址。

参考资料

上一篇 下一篇

猜你喜欢

热点阅读