浏览器根对象navigator之对象属性概览

第1章 connection[试验]

navigator.connection 是只读的,提供一个NetworkInformation 对象来获取设备的网络连接信息。例如用户设备的当前带宽或连接是否被计量, 这可以用于基于用户的连接来选择高清晰度内容或低清晰度内容。

1.1 使用

下面是一个侦测用户设备连接状态变化的例子:

var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;

var type = connection.type;

function updateConnectionStatus() {

  console.log("设备的网络连接从" + type + "变成了" + connection.type);

}

connection.addEventListener('typechange', updateConnectionStatus);

1.2 事件

与online和offline事件有关系。

第2章 plugins

返回一个 PluginArray 类型的对象,包含了当前所使用的浏览器安装的所有插件。该属性不属于任何一个标准。

2.1 使用

下例演示了如何在一个页面上输出浏览器上安装的所有插件。 Plugin对象的属性有: name, filename, description, 和 version:

var L = navigator.plugins.length;

document.write(L.toString().bold() + " Plugin(s)".bold());

document.write("<br>");

document.write("Name | Filename | description".bold());

document.write("<br>");

for(var i=0; i<L; i++) {

    document.write(navigator.plugins[i].name);

    document.write(" | ");

    document.write(navigator.plugins[i].filename);

    document.write(" | ");

    document.write(navigator.plugins[i].description);

    document.write(" | ");

    document.write(navigator.plugins[i].version);

    document.write("<br>");

}

 

第3章 mimeTypes

返回一个MimeTypeArray对象,其中包含可被当前浏览器识别的MimeType对象的列表。

mimeTypes 是一个 MimeTypeArray 对象,其中含有 length 属性、item(index) 和 namedItem(name) 方法。

3.1 示例

下面介绍了如何使用mimeTypes:

function isJavaPresent() {

  return 'application/x-java-applet' in navigator.mimeTypes;

}

 

function getJavaPluginDescription() {

  var mimetype = navigator.mimeTypes['application/x-java-applet'];

  if (mimetype === undefined) {

    // no Java plugin present

    return undefined;

  }

  return mimetype.enabledPlugin.description;

}

 

第4章 serviceWorker[试验]

serviceWorker是navigator的只读属性,返回ServiceWorkerContainer对象,它提供ServiceWorker的注册,删除,升级和通信的访问。

4.1 ServiceWorkerContainer

ServiceWorkerContainer接口为 service worker提供一个容器般的功能,包括对service worker的注册,卸载 ,更新和访问service worker的状态,以及他们的注册者。

相关详情参见MDN网站

第5章 credentials[试验]

credentials属性返回CredentialsContainer接口,该接口可用于特征检测。

if ('credentials' in navigator) {

  navigator.credentials.get({password: true})

  .then(function(creds) {

    //Do something with the credentials.

  });

} else {

  //Handle sign-in the way you did before.

};

5.1 CredentialsContainer

Credential Management API 的 CredentialsContainer 接口提供了请求 credentials 和通知用户代理(当成功登陆或登出事件发生时)的方法。可通过  Navigator.credentials 获得该接口。

第6章 permissions[试验]

permissions 是 Navigator 读属性,返回一个可用于查询或更新某些APIs(由Permissions API覆盖)的权限状态的对象。

navigator.permissions.query({name:'geolocation'}).then(function(result) {

  if (result.state === 'granted') {

    showMap();

  } else if (result.state === 'prompt') {

    showButtonToEnableMap();

  }

  // 如果被拒绝,请不要做任何操作。

});

参见:Web Permissions API

第7章 geolocation

geolocation 只读属性返回一个 Geolocation 对象,通过这个对象可以访问到设备的位置信息。这允许网站或应用根据用户的位置提供个性化结果。

第8章 mediaDevices

mediaDevices 是 Navigator 只读属性,返回一个 MediaDevices 对象,该对象可提供对相机和麦克风等媒体输入设备的连接访问,也包括屏幕共享。

'use strict';

 

// Put variables in global scope to make them available to the browser console.

var video = document.querySelector('video');

var constraints = window.constraints = {

    audio: false,

    video: true

};

var errorElement = document.querySelector('#errorMsg');

 

navigator.mediaDevices.getUserMedia(constraints)

    .then(function(stream) {

        var videoTracks = stream.getVideoTracks();

        console.log('Got stream with constraints:', constraints);

        console.log('Using video device: ' + videoTracks[0].label);

        stream.onended = function() {

            console.log('Stream ended');

        };

        window.stream = stream; // make variable available to browser console

        video.srcObject = stream;

    })

    .catch(function(error) {

        if (error.name === 'ConstraintNotSatisfiedError') {

            errorMsg('The resolution ' + constraints.video.width.exact + 'x' +

                constraints.video.width.exact + ' px is not supported by your device.');

        } else if (error.name === 'PermissionDeniedError') {

            errorMsg('Permissions have not been granted to use your camera and ' +

                'microphone, you need to allow the page access to your devices in ' +

                'order for the demo to work.');

        }

        errorMsg('getUserMedia error: ' + error.name, error);

    });

 

function errorMsg(msg, error) {

    errorElement.innerHTML += '<p>' + msg + '</p>';

    if (typeof error !== 'undefined') {

        console.error(error);

    }

}

 

第9章 其他

budget、usb、storage、presentation、webkitTemporaryStorage、webkitPersistentStorage等在谷歌中存在,而火狐并未实现,暂未了解。

posted @ 2017-02-22 18:46  nDos  阅读(753)  评论(0编辑  收藏  举报