javascript - 如何检测网络应用程序中的事件漫游模式?

标签 javascript service-worker progressive-web-apps

是否有任何方法可以从客户端 JavaScript 检测到当前连接是蜂窝连接,或者更好的是,处于漫游模式?我想通过在渐进式网络应用程序中选择最佳更新策略来节省用户的流量消耗。

最佳答案

Network Information API它可以尝试告诉你网络类型。请注意,设备可能在 wifi 上同时连接到蜂窝数据盘,因此不能保证其准确性。

let preloadVideo = true;
var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
if (connection) {
  if (connection.effectiveType=== 'cellular') {
    preloadVideo = false;
  }
}

我还建议查看 Save-Data可以告诉您浏览器是否设置为减少数据使用量的 API。

if ("connection" in navigator) {
    if (navigator.connection.saveData === true) {
        // Implement data saving operations here.
    }
}

关于javascript - 如何检测网络应用程序中的事件漫游模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59198882/

相关文章:

javascript - html中选择列表上的弹出文本

javascript - 在 gulp 中分割任务

javascript - 从内容脚本打开一个 chrome 选项卡

google-analytics - 在支持离线功能的网络应用程序中处理 Google 分析

javascript - iOS 渐进式 Web 应用程序深色模式启动图像?

android - React PWA 无法在 Chrome 中打开外部 PDF(Android)

javascript - 按下回车键时不刷新页面

javascript - 了解 ServiceWorker 注册

google-chrome - 未通过 Service Worker 传递推送通知

node.js - 渐进式网络应用程序(PWA) : Issue in setting start_url of manifest