互联网技术 · 2024年2月23日

HTML5+ API plusready的兼容性疑问

这篇文章主要介绍了HTML5+ API plusready的兼容问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

Android平台提前注入5+ API,支持在plusready事件前调用

在5+ Runtime环境中,通常情况下需要html页面解析完成后才会注入5+ API,执行的顺序为:

1. 加载html页面

2. 解析html页面(下载script/link等节点引用的资源,如js/css文件)

3. 触发DOMContentLoaded事件

4. 注入5+ API

5. 触发plusready事件

这样导致5+ API生效时间比较延后,在html中引用js执行之后才能调用5+ API,通常采用以下代码调用5+ API:

document.addEventListener(plusready,function () {
// 在这里调用5+ API
// 如获取设备唯一标识 plus.device.uuid
},false);

但是在新版本中,将支持提前注入5+ API,可以在plusready事件触发之前调用5+ API,提前引入节点:

<script src=”html5plus://ready”></script>

兼容写法:

if(window.plus){
// 在这里调用5+ API
}else{// 兼容老版本的plusready事件
document.addEventListener(plusready,function () {
// 在这里调用5+ API
},false);
}

注意

1. 仅仅是提前注入5+ API,并不会提前触发plusready事件(仅步骤4提前操作了)

2. Android3.0及以上平台才支持提前注入,Android2.*版本无法提前注入

3. 在流应用环境中的wap2app会自动提前注入(第一次引用网络js时注入),不需要添加

来源:脚本之家

链接:https://www.jb51.net/html5/753136.html

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册