2017-08-28 6 views
0

モバイルSafariとIEで次のエラーが発生します。Javascript startsWith iPhoneとIEで動作しません

TypeError: undefined is not a function 

問題のコードは次のとおりです。

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

これはChromeとFirefoxで美しく動作します。 Safari & IEはなぜそれが好きではないか考えてみましょうか?

+1

を追加することができます両方のブラウザは、([MDN]によると、 'String.prototype.startsWith'をサポートしていませんhttps://developer.mozilla.org/en/docs/Web/ JavaScript/Reference/Global_Objects/String/startsWith)を参照してください。 – raina77ow

+0

よくsh * t ..そう、その問題の 'startsWith' .. – ToddT

+0

ああ私はそのリンクで定義されているPolyfillメソッドとそのうまくいきました。正しい方向に私を指してくれてありがとう! – ToddT

答えて

1

次のポリフィル

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

正確に..ありがとう! – ToddT

関連する問題