2012-10-16 6 views
8

Ricevo un errore su IE8 per i seguenti javascript:IE8 Date() compatibilità errore

<script type="text/javascript"> 
    //when html doc is all ready 
    $(document).ready(function() { 
     var socket = io.connect(); 
     var room = 'public'; 
     socket.emit('join', room); 

     socket.on('message', function (data) { 
      var output = ''; 
      output += '<div class="trace-content">'; 
      output += ' <div class="mname">' + data.name + '</div>'; 
      output += ' <div class="mdate">' + data.date + '</div>'; 
      output += ' <p class="mtext">' + data.message + '</p>'; 
      output += '</div>'; 

      $(output).prependTo('#traces'); 
     }); 

     $('button').click(function() { 
      var date = new Date().toISOString(); 
      socket.emit('message', { 
       name: $('#name').val(), 
       message: $('#message').val(), 
       date: date.slice(2,10) + ' ' + date.slice(11, 19) 
      }); 
     }); 
    }); 
</script> 

Il problema sembra essere in prima linea:. Var data = new Date() toISOString(); Ho problemi con il pin che indica esattamente il problema. Tutto il resto sembra funzionare bene; solo quel pulsante e il codice che segue. Qualche idea?

risposta

24

IE8 non supporta .toISOString(). È possibile utilizzare questo codice come uno spessore (da Mozilla):

if (!Date.prototype.toISOString) {   
    (function() {   
     function pad(number) { 
      var r = String(number); 
      if (r.length === 1) { 
       r = '0' + r; 
      } 
      return r; 
     }  
     Date.prototype.toISOString = function() { 
      return this.getUTCFullYear() 
       + '-' + pad(this.getUTCMonth() + 1) 
       + '-' + pad(this.getUTCDate()) 
       + 'T' + pad(this.getUTCHours()) 
       + ':' + pad(this.getUTCMinutes()) 
       + ':' + pad(this.getUTCSeconds()) 
       + '.' + String((this.getUTCMilliseconds()/1000).toFixed(3)).slice(2, 5) 
       + 'Z'; 
     };  
    }()); 
} 
+0

Bel pezzo di codice semplice. Grazie! – dk123

3

Naturalmente si ottiene un errore http://kangax.github.com/es5-compat-table. Cerca date.prototype.toISOString() polyfill in Google. Ho trovato questo https://gist.github.com/1044533

Dal succo:

// thanks to @fgnass and @subzey for their awesome golf skills 
// annotation by @fgnass 

function(a){ 
    a=this; 
    return (
    1e3 // Insert a leading zero as padding for months < 10 
    -~a.getUTCMonth() // Months start at 0, so increment it by one 
    *10 // Insert a trailing zero as padding for days < 10 
    +a.toUTCString() // Can be "1 Jan 1970 00:00:00 GMT" or "Thu, 01 Jan 1970 00:00:00 GMT" 
    +1e3+a/1 // Append the millis, add 1000 to handle timestamps <= 999 
    // The resulting String for new Date(0) will be: 
    // "-1010 Thu, 01 Jan 1970 00:00:00 GMT1000" or 
    // "-10101 Jan 1970 00:00:00 GMT1000" (IE) 
    ).replace(
     // The two digits after the leading '-1' contain the month 
     // The next two digits (at whatever location) contain the day 
     // The last three chars are the milliseconds 
     /1(..).*?(\d\d)\D+(\d+).(\S+).*(...)/, 
    '$3-$1-$2T$4.$5Z') 
} 

Nota: questo potrebbe non essere il codice più leggibile o miglior esempio di ovatta ma sembra di lavorare secondo i commenti nel succo quindi è un rapida soluzione di copia/incolla.

+0

Wow, non vorrei mantenere quel codice :) (Il codice del golf sta cercando di ottenere qualcosa nella minor quantità di caratteri). – Bill

+0

È un esempio di 140 byte leggendo i commenti. Il mio punto è mostrare OP ciò che dovrebbe cercare. Questo è pensato per essere una soluzione di copia/incolla. I commenti sembrano implicare che ci sono opzioni migliori, ma la gente segnala che funziona in IE6 e altri browser. – elclanrs

+0

Grazie per la risposta dettagliata. Sicuramente cercherò in quel tavolo che hai collegato. Ho scelto di accettare la risposta di Bill anche se solo per il fatto che avrò difficoltà a ricordare che cosa potrebbe fare un codice di questa complessità in momenti futuri; ottimo aiuto, grazie! – dk123

0

Perché non stai utilizzando lo toJSON method? È supportato da IE8.

+0

Questo non fornisce una risposta alla domanda. Per criticare o richiedere chiarimenti da un autore, lascia un commento sotto il loro post. - [Dalla recensione] (/ recensione/post di bassa qualità/10597816) –

+0

@JonSurrell hai provato a utilizzare tale metodo? Risolve il problema poiché [chiamando 'toJSON' restituisce una stringa che rappresenta il valore dell'oggetto Date] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON#Description). – Knu