Javascript startsWith 不适用于 iPhone 和 IE

标签 javascript internet-explorer safari

我仅在移动版 Safari 和 IE 上收到以下错误。

TypeError: undefined is not a function

违规代码是:

if(window.location.origin.startsWith(shopAddress + "/account/login")  && 
sessionStorage.getItem('loggedIn') == "true")

这在 Chrome 和 Firefox 中运行良好。知道为什么 Safari 和 IE 不喜欢它吗?

最佳答案

你可以添加以下polyfill

if(!String.prototype.startsWith) {
  String.prototype.startsWith = function(searchString, position){
    return this.substr(position || 0, searchString.length) === searchString;
  };
}

关于Javascript startsWith 不适用于 iPhone 和 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45922506/

相关文章:

javascript - 如何在 Node.js 上使用 twitter api 时实现回调函数

java - Selenium 网络驱动程序 : Suppress IE log info prints on the console

internet-explorer - 媒体查询不适用于 ie10

internet-explorer - 有谁知道用于在IE 8上查看/编辑Cookie的实用程序?

javascript - Safari 阻止视频上的 play() 尽管被点击事件调用

safari - 适用于 Web 设计师的 Windows 或 Linux 上的真实 Safari 模拟器

javascript - 当它是包含对象的数组时如何在 JSDoc 中记录变量?

javascript - 根据键值显示 JSON 文件中的值

javascript - HTML5 的 Canvas + Javascript 代码在 iOS 上不起作用

javascript - 使用javascript获取电脑的时间并将其保存在数据库中