79

Sto verificando l'url per vedere se contiene o include un? in esso per controllare lo stato pop dell'hash nella finestra. Tutti gli altri browser non hanno un problema solo IE.IE11 - L'oggetto non supporta la proprietà o il metodo 'includes' - javascript window.location.hash

Il debugger mi dà questo errore quando provo a caricare in questo modo oggetto non supporta la proprietà o il metodo 'include'

ottengo alcun errore quando carica la pagina in tghrough il popstate

$(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

Qual è il valore di 'window.location.hash' in Internet Explorer 11? – nils

+3

TL; Risposta DR: usare .indexOf ('')> = 0 invece di .includes ('') – Andrew

risposta

116

In base allo MDN reference page, includes non è supportato su Internet Explorer. L'alternativa più semplice è quella di utilizzare indexOf, in questo modo:

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

OK bene ora non riesco a farlo funzionare se (window.location.hash.indexOf ("?")> = 0) { \t \t \t \t // non fare nulla \t \t \t} else { \t \t \t \t location.href = location.href + '?'; \t \t \t} –

+0

Devo aggiungere un? alla fine dell'URL se l'url non ne ha già uno –

+0

Grazie. Questo ha risolto il mio problema. –

2

Come in Internet Explorer, il metodo javascript "comprende" non supporta che sta portando l'errore, come di seguito

dijit.form.FilteringSelect TypeError: l'oggetto non supporta la proprietà o il metodo 'include'

Così ho cambiato il metodo di stringa JavaScript da "include" a "indexOf", come di seguito

//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 implementa String.prototype.include quindi perché non utilizzare Polyfill ufficiale?

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; 
     } 
    }; 
    } 

Fonte

: polyfill source

+0

potresti anche usare il polyfill da core-js trovato qui: https: // github.com/zloirock/core-js # stage-4-proposal –

2

Ho usato includes da Lodash che è davvero simile a quella nativa.

0

Questa domanda e le sue risposte mi ha portato a mia soluzione (con l'aiuto di SO), anche se alcuni dicono che non dovrebbe interferire con i prototipi nativi:

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

Poi ho solo sostituito tutti .includes() con .doesInclude() e il mio problema è stato risolto.

+1

'.includes()' funziona sia su stringhe che su array. La vostra soluzione 'substring()' funziona solo con le stringhe e fallisce con gli array. Come risposta da altre persone, usare 'indexOf()' invece di 'substring()' è migliore perché funziona in entrambi i casi. –

0

Mi trovavo di fronte a un problema simile durante la generazione di eventi Keyboard in IE e l'invio alla casella di input.

Così passato a jQuery e funziona bene, jQuery si occupa di tutti i dettagli interni.