Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atob not working in IE

I am trying to use atob for converting base64 encoded data to string. I found its working fine in Mozilla and Chrome, but it is not working in IE. Please tell me any substitue for this, or how to make this work in IE

like image 789
Paritosh Singh Avatar asked Jul 17 '12 14:07

Paritosh Singh


People also ask

Is ATOB deprecated?

btoa and atob are only deprecated for Node JS. If you prepend the window. you will get rid of this deprecation mark. On the other hand, if you are trying to use btoa o atob in the back-end side, you definitely should use Buffer interface.

Why is ATOB called ATOB?

It's because the 'b' in a atob stands for binary, not base64. ASCII is base64 encoded, and strings are binary.

What is the difference between base64 and UTF 8?

UTF-8 is like the other UTF encodings a character encoding to encode characters of the Unicode character set UCS. Base64 is an encoding to represent any byte sequence by a sequence of printable characters (i.e. A – Z , a – z , 0 – 9 , + , and / ). There is no System.

What does ATOB do in Javascript?

atob() The atob() function decodes a string of data which has been encoded using Base64 encoding. You can use the btoa() method to encode and transmit data which may otherwise cause communication problems, then transmit it and use the atob() method to decode the data again.


1 Answers

Go to this link and include js present there:

http://code.google.com/p/stringencoders/source/browse/trunk/javascript/base64.js?r=230

/*  * Copyright (c) 2010 Nick Galbreath  * See full license on http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript  */  var base64 = {}; base64.PADCHAR = '='; base64.ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';  base64.makeDOMException = function() {     // sadly in FF,Safari,Chrome you can't make a DOMException     var e, tmp;      try {         return new DOMException(DOMException.INVALID_CHARACTER_ERR);     } catch (tmp) {         // not available, just passback a duck-typed equiv         // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error         // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error/prototype         var ex = new Error("DOM Exception 5");          // ex.number and ex.description is IE-specific.         ex.code = ex.number = 5;         ex.name = ex.description = "INVALID_CHARACTER_ERR";          // Safari/Chrome output format         ex.toString = function() { return 'Error: ' + ex.name + ': ' + ex.message; };         return ex;     } }  base64.getbyte64 = function(s,i) {     // This is oddly fast, except on Chrome/V8.     //  Minimal or no improvement in performance by using a     //   object with properties mapping chars to value (eg. 'A': 0)     var idx = base64.ALPHA.indexOf(s.charAt(i));     if (idx === -1) {         throw base64.makeDOMException();     }     return idx; }  base64.decode = function(s) {     // convert to string     s = '' + s;     var getbyte64 = base64.getbyte64;     var pads, i, b10;     var imax = s.length     if (imax === 0) {         return s;     }      if (imax % 4 !== 0) {         throw base64.makeDOMException();     }      pads = 0     if (s.charAt(imax - 1) === base64.PADCHAR) {         pads = 1;         if (s.charAt(imax - 2) === base64.PADCHAR) {             pads = 2;         }         // either way, we want to ignore this last block         imax -= 4;     }      var x = [];     for (i = 0; i < imax; i += 4) {         b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) |             (getbyte64(s,i+2) << 6) | getbyte64(s,i+3);         x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff, b10 & 0xff));     }      switch (pads) {     case 1:         b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) | (getbyte64(s,i+2) << 6);         x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff));         break;     case 2:         b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12);         x.push(String.fromCharCode(b10 >> 16));         break;     }     return x.join(''); }  base64.getbyte = function(s,i) {     var x = s.charCodeAt(i);     if (x > 255) {         throw base64.makeDOMException();     }     return x; }  base64.encode = function(s) {     if (arguments.length !== 1) {         throw new SyntaxError("Not enough arguments");     }     var padchar = base64.PADCHAR;     var alpha   = base64.ALPHA;     var getbyte = base64.getbyte;      var i, b10;     var x = [];      // convert to string     s = '' + s;      var imax = s.length - s.length % 3;      if (s.length === 0) {         return s;     }     for (i = 0; i < imax; i += 3) {         b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8) | getbyte(s,i+2);         x.push(alpha.charAt(b10 >> 18));         x.push(alpha.charAt((b10 >> 12) & 0x3F));         x.push(alpha.charAt((b10 >> 6) & 0x3f));         x.push(alpha.charAt(b10 & 0x3f));     }     switch (s.length - imax) {     case 1:         b10 = getbyte(s,i) << 16;         x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +                padchar + padchar);         break;     case 2:         b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8);         x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +                alpha.charAt((b10 >> 6) & 0x3f) + padchar);         break;     }     return x.join(''); } 
like image 169
Paritosh Singh Avatar answered Oct 12 '22 09:10

Paritosh Singh