79

İçerdiği veya içerdiğini görmek için URL'yi kontrol ediyorum? pencerede karma pop durumunu kontrol etmek için. Diğer tüm tarayıcılarda yalnızca IE sorunu yoktur.IE11 - Nesne özelliği veya yöntemi desteklemiyor 'içerir' - javascript window.location.hash

Ben tghrough popstate sayfayı yüklediğinizde özelliği veya yöntemi 'içerir'

hiçbir hata alıyorum desteklemeyen bu şekilde Nesnesi'nde yüklemeye çalıştığınızda ayıklayıcı bana bu hata veriyor

$(document).ready(function(e) { 
     if(window.location.hash) { 
      var hash; 
      if(window.location.hash.includes("?")) { 
       alert('I have a ?'); 
       hash = window.location.hash.substring(window.location.hash.indexOf('#') + 0,window.location.hash.indexOf('?')); 
      }else { 
       hash = window.location.hash; 
      }; 
      if (hash=="#DRS" || hash=="#DRP" || hash=="#DFFI" || hash=="#DCI" || hash=="#DCP" || hash=="#DRP" || hash=="#DRMA" || hash=="#EICS" || hash=="#ORG"){ 
       $(hash+'Content').addClass('pageOn').removeClass('pageOff'); 
      }else { 
       $('#homeContent').addClass('pageOn').removeClass('pageOff'); 
      }; 
     } else { 
      $('#homeContent').addClass('pageOn').removeClass('pageOff'); 
     } 
     $(window).on('popstate', function() { 
      var hash; 
      if(window.location.hash.includes("?")) { 
       hash = window.location.hash.substring(window.location.hash.indexOf('#') + 0,window.location.hash.indexOf('?')); 
      }else { 
       hash = window.location.hash; 
      }; 
      if (hash=="#DRS" || hash=="#DRP" || hash=="#DFFI" || hash=="#DCI" || hash=="#DCP" || hash=="#DRP" || hash=="#DRMA" || hash=="#EICS" || hash=="#ORG"){ 
       $(this).navigate({target: $(hash+'Content')}); 
       if(window.location.hash.includes("?")) { 
       }else{ 
        location.href = location.href+'?'; 
       } 
      }else { 
       $(this).navigate({target: $('#homeContent')}); 
      }; 
     }); 
}); 
+0

Internet explorer 11'deki window.location.hash 'değeri nedir? – nils

+3

TL; DR yanıtı: .indexOf ('')> = 0 yerine .includes ('') – Andrew

cevap

116

MDN reference page'a göre, Internet Explorer'da includes desteklenmez. En basit alternatif böyle, indexOf kullanmaktır:

if(window.location.hash.indexOf("?") >= 0) { 
    ... 
} 
+1

Tamam, şimdi şunu çalıştıramıyorum (window.location.hash.indexOf ("?")> = '?' 0) else { \t \t \t \t location.href = location.href + { \t \t \t \t // \t \t \t hiçbir şey}; \t \t \t} –

+0

Ekleyeceğim bir? url'nin sonuna bir URL zaten sahip değilse –

+0

Teşekkür ederiz. Bu benim sorunumu çözdü. –

2

Internet Explorer As javascript yöntemi "içeren"

dijit.form.FilteringSelect aşağıdaki gibi hataya neden olan desteklemiyor TypeError: özelliği veya yöntemi desteklemiyor Nesne

yüzden aşağıda olarak "indexOf" "içeren" JavaScript dize yöntemi değişti 'içeren'

//str1 doesn't match str2 w.r.t index, so it will try to add object 
var str1="acd", str2="b"; 
if(str1.indexOf(str2) == -1) 
{ 
    alert("add object"); 
} 
else 
{ 
alert("object not added"); 
} 
21

IE11, String.prototype.includes dosyasını uygular, bu nedenle resmi Polyfill'i neden kullanmıyorsunuz?

if (!String.prototype.includes) { 
    String.prototype.includes = function(search, start) { 
     if (typeof start !== 'number') { 
     start = 0; 
     } 

     if (start + search.length > this.length) { 
     return false; 
     } else { 
     return this.indexOf(search, start) !== -1; 
     } 
    }; 
    } 

Kaynak: Ben yerli gerçekten benzerdir Lodash den includes kullandım polyfill source

+0

Ayrıca burada bulunan core-js polyfill kullanabilirsiniz: https: // github.com/zloirock/core-js # stage-4-proposals –

2

.

// IE does not support .includes() so I'm making my own: 
    String.prototype.doesInclude=function(needle){ 
    return this.substring(needle) != -1; 
    } 
Sonra

Sadece .doesInclude() tüm .includes() değiştirilir ve: Bazı sen yerli prototipleri ile karışmaması gereken söylese de

0

bu soru ve cevaplar, (SO yardımıyla) kendi çözüme götürdü benim problemim çözüldü.

+1

'.includes()' hem dizelerde hem de dizilerde çalışır. 'Substring()' çözümünüz yalnızca dizelerle çalışır ve dizilerle başarısız olur. Diğer insanlar tarafından yanıtlandığı gibi, 'indexOf()' 'substring()' yerine kullanmak daha iyidir çünkü bu her iki durumda da çalışır. –

0

IE'de Keyboard olayı oluştururken ve bunu giriş kutusuna gönderirken benzer sorunlarla karşılaşıyordum.

jQuery'ye geçti ve gayet iyi çalışıyor, jQuery tüm iç ayrıntılarla ilgileniyor.