【问题标题】:What's the best way to handle non-supported browsers in Dart?在 Dart 中处理不受支持的浏览器的最佳方法是什么?
【发布时间】:2023-03-04 15:37:01
【问题描述】:

Dart 在某些浏览器上不受支持,尤其是某些移动浏览器,例如现有的 Android 浏览器。我想显示一条“不支持”的消息,而不是消失。

所以我们有一些 JS 会抛出一个带有“不支持”消息的 DIV,如果它检测到所谓的不受支持的设备:

var ua = navigator.userAgent.toLowerCase();

// Android 1-4
if (/android [1-4]/i.test(ua)) {
  window.document.querySelector('#mobile-not-supported').style.display = '';
  window.document.querySelector('#application-root-container').style.display = 'none';
}

问题在于,它在某些实际工作的设备上显示。例如,在某些 Android 设备上,它可以在 Chrome 浏览器上运行,而不是在普通浏览器上运行。

这里最好的方法是什么?我什至不确定支持的浏览器的严格列表(只有这个:https://www.dartlang.org/support/faq.html#q-what-browsers-do-you-support-as-javascript-compilation-targets)。理想情况下,JS 有一种方法可以检测到应用程序的某些方面不工作,并且在这些情况下只显示“不支持”消息。

【问题讨论】:

  • 不要使用用户代理字符串,而是使用supported 属性。此属性存在于许多 dart:html 对象上。
  • 很好,不知道。同时,将我自己的解决方案作为单独的答案发布,其中包括专门在用户代理字符串中查找原生 Android 浏览器。
  • 抱歉,你会牺牲所有使用安卓浏览器的人,只是为了使用 Dart,因为它存在大量低采用率问题和不成熟(目前)?
  • @BenjaminGruenbaum 简而言之:是的,就目前而言,因为它是一个现代应用程序,我们足够小,切换到 Chrome 应用程序并不是一个很大的要求。许多现代网络应用程序无法在原生 Android 上运行。不过,稍后我想要一个更好的解决方案。

标签: javascript dart


【解决方案1】:

我改进的解决方案是在用户代理字符串中专门寻找原生 Android。

var ua = navigator.userAgent.toLowerCase();

// Look for native Android browser, which Dart does not support.
var is_android = ((ua.indexOf('mozilla/5.0') > -1 && ua.indexOf('android ') > -1 &&    ua.indexOf('applewebkit') > -1) && !(ua.indexOf('chrome') > -1));
if (is_android) {
  window.document.querySelector('#mobile-not-supported').style.display = '';
  window.document.querySelector('#application-root-container').style.display = 'none';
}

以后我可能会试试@caffinatedmonkey 提到的supported 属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多