浏览器兼容性:JavaScript polyfill

news/2024/10/21 10:18:41/

由于 JavaScript API 有自己的规范,因此并非所有浏览器都同时支持特定规范。因此,我们必须提供 API 的补丁版本,以确保它仍然可以在不原生支持它的浏览器上运行。这种补丁称为 polyfill。

比如为 Safari 15.4 之前提供 Array 的at()方法的补丁:

Array.prototype.at =Array.prototype.at ||function (index) {// The implementation ...};

又比如在Safari 15.4 之前 structedClone不可用,也需要打上补丁:

typeof window.structuredClone !== 'function' &&window.structuredClone = function (value) {// ...};

出于为 CSS 属性添加前缀的相同原因,浏览器也在实验阶段为 JavaScript API 添加前缀。下表列出了最流行的浏览器的前缀: 

前缀浏览器
mozfirefox
ms使用 Chromium 引擎之前的 Internet Explorer 和 Microsoft Edge
o使用 WebKit 引擎之前的 Opera 旧版本
webkitGoogle Chrome、Safari、更新版本的 Opera 和 Edge 以及几乎所有的 iOS 浏览器

比如,针对requestAnimationFrame函数:

window.requestAnimationFrame =window.requestAnimationFrame ||window.mozRequestAnimationFrame ||window.webkitRequestAnimationFrame ||window.oRequestAnimationFrame ||window.msRequestAnimationFrame;

全屏 API 则是更复杂的场景,支持全屏模式必须涵盖以下功能:

标准API描述
fullscreenEnabled检查是否启用了全屏模式
requestFullscreen请求浏览器进入全屏模式
fullscreenElement返回全屏元素
fullscreenchange全屏模式发生时触发该事件
exitFullscreen退出全屏模式

首先需要一个支持该接口的包装器: 

enum Api {ExitFullScreen,FullScreenChange,FullScreenElement,FullScreenEnabled,RequestFullScreen,
}type ApiMap = {[key in keyof typeof Api]: string;
};

针对不同浏览器,定义每个API的包装器:

const defaultVendor: ApiMap = {ExitFullScreen: 'exitFullscreen',FullScreenChange: 'fullscreenchange',FullScreenElement: 'fullscreenElement',FullScreenEnabled: 'fullscreenEnabled',RequestFullScreen: 'requestFullscreen',
};const webkitVendor: ApiMap = {ExitFullScreen: 'webkitExitFullscreen',FullScreenChange: 'webkitfullscreenchange',FullScreenElement: 'webkitFullscreenElement',FullScreenEnabled: 'webkitFullscreenEnabled',RequestFullScreen: 'webkitRequestFullscreen',
};const mozVendor: ApiMap = {ExitFullScreen: 'mozCancelFullScreen',FullScreenChange: 'mozfullscreenchange',FullScreenElement: 'mozFullScreenElement',FullScreenEnabled: 'mozFullScreenEnabled',RequestFullScreen: 'mozRequestFullScreen',
};const msVendor: ApiMap = {ExitFullScreen: 'msExitFullscreen',FullScreenChange: 'msFullscreenChange',FullScreenElement: 'msFullscreenElement',FullScreenEnabled: 'msFullscreenEnabled',RequestFullScreen: 'msRequestFullscreen',
};

最后就可以得到当前浏览器支持的API集,直接简单地调用给定元素的全屏AP即可:

const vendor: ApiMap =(Api.FullScreenEnabled in document && defaultVendor) ||(webkitVendor.FullScreenEnabled in document && webkitVendor) ||(msVendor.FullScreenEnabled in document && msVendor) ||defaultVendor;element[vendor.RequestFullScreen]();

http://www.ppmy.cn/news/106470.html

相关文章

防雷接地的施工工艺与防雷施工方案

雷电是自然界的一种强大而危险的自然现象,经常造成重大财产损失和人员伤亡。为了保护建筑物和人员免受雷电的危害,防雷接地系统的设计和施工至关重要。本文将介绍防雷接地的施工工艺和防雷施工方案,强调专业和符合国家标准的方法,…

【SA8295P 源码分析】10 - HQX Display(OpenWFD)介绍

【SA8295P 源码分析】10 - HQX Display(OpenWFD)介绍 一、HQX Display 介绍1.1 OpenWF Display Driver二、HQX Display Bringup2.1 qcdisplaycfg.xml 配置文件2.1 配置两个 DPUs in QNX2.1.1 配置 graphics_ADP_STAR.conf2.1.2 配置 qcdisplaycfg_xxx.xml系列文章汇总见:《【…

Nodejs安装以及环境变量配置

Nodejs安装以及环境变量配置 1.下载Nodejs1.1最新版下载1.2历史版本下载 2.安装3.验证4.环境变量配置5.npm下载设置6.测试6.设置国内镜像提高下载速度 1.下载Nodejs 1.1最新版下载 直接官网下载:https://nodejs.org/zh-cn/download 根据自己的需求,选择…

dmidecode详解

dmidecode是Linux下的一款工具,它可以读取DMI(Desktop Management Interface)表,获取硬件信息。 DMI表包含了主机板、BIOS和系统硬件等信息。dmidecode通过读取DMI表,可以获取硬件厂商信息、型号、序列号、BIOS版本以及内存、CPU等详细信息。 dmidecode的基本语法是: dmidecod…

计算机底层的密码读书笔记

线程池的理解协程的理解高并发服务器的考量内存管理函数调用与栈影响多线程性能的缓存问题 线程池的理解 重复创建和销毁线程会存在开销,线程过多会消耗大量内存,较多线程之间的切换也存在开销 线程池用来复用线程,控制线程数量 线程池中…

Redis 事务详细介绍

事务 注意:Redis单条命令是保证原子性的;但是事务不保证原子性! Redis事务没有隔离级别的概念,所有的命令在事务中,并没有直接被执行,只有发起执行命令时才执行 Redis事务本质:一组命令的集合&…

MyBatis的部分知识点

一、resultMap的constructor配置方式 <resultMap id"" type""> <constructor> <!--主键--> <idArg column"id" javaType"_int"/> <!--其他列--> …

测试报告模板二

项目名称 系统测试报告 平台测试小组 2023年x月xx日 文档信息 文档名称: 作者: