1

I know there is already a question on here with solutions for this but I have little understanding of javascript and I can't seem to figure out what they are saying to add in or where. The link to it is here: IExplorer: SCRIPT438: Object doesn't support property or method 'btoa'

Can someone explain to me what they are doing to make it work? Thanks.

Community
  • 1
  • 1
Joshua
  • 23
  • 1
  • 6
  • Not all browsers support .btoa(), so you have to polyfill it for non-supporting browser by adding [this script](https://github.com/davidchambers/Base64.js/blob/master/base64.js) – adeneo Jan 30 '14 at 17:13
  • Somewhere, your script (or a library that your script uses) tries to call `window.btoa`. That function doesn't exist in older versions of IE. Instead, you must include a separate JS file (in a ` – apsillers Jan 30 '14 at 17:15
  • @adeneo do I just add this into my file and it should work or do I need to call to this? – Joshua Jan 30 '14 at 17:23

2 Answers2

4

Older browsers may not support Window.bota, which is basically an oddly-named method to convert strings to base64 representations, as you probably know.

Making new functionality available in older browsers is called "polyfilling". Put the script base64.js (download) or base64.min.js (download) on your website (I'm going to assume you're using the latter, and putting it in the /js/vendor directory), and reference it thusly (before you need to use Window.bota):

<script src="/js/vendor/base64.min.js"></script>

If the browser is newer, this script won't do anything (i.e., it won't replace the existing Window.btoa implementation). If the browser is older, it will now have the functionality.

If you want to avoid the additional HTTP request required to read base64.min.js, you can use yepnope:

yepnope({
  test: window.btoa && window.atob,
  nope: '/js/vendor/base64.js',
  callback: function () {
    // `btoa` and `atob` are now safe to use
  }
});
Ethan Brown
  • 26,892
  • 4
  • 80
  • 92
  • Joshua, it's really hard to answer that question without knowing what you're doing, or what motivated your original question. If you need to use `Window.btoa`, you must add the polyfill _before_ you use `Window.btoa`...I'm not sure what else you're confused about. – Ethan Brown Jan 30 '14 at 17:47
  • If I use the base64 files do I need to add the yepnope function? But I believe anyone using this program will have a newer browser so can I just use the yepnope function without base64? – Joshua Jan 30 '14 at 17:50
  • ...I just don't understand if I add the polyfill inside the function or if its meant to go outside the function? – Joshua Jan 30 '14 at 17:51
  • As I indicate in my answer, using yepnope is optional: that will just save an extra HTTP request if they're using a newer browser. (However, if this is the only thing you're using yepnope for, then you've got the HTTP request to load yepnope, so you're not gaining anything there). Sounds to me like you should ignore yepnope, and just include `base64.min.js` and call it a day. I'm just trying to give you the complete picture. – Ethan Brown Jan 30 '14 at 18:05
  • Okay thanks a lot! One more question though...When I'm calling to the script for base64 where exactly do I do this? My script for the tableToExcel function is in its own jscript file btw. – Joshua Jan 30 '14 at 18:10
  • You need to call that before you link in your `tableToExcel` function. For example, if your script is in `/js/tableToExcel.js`, and you link it in in the `` section of your HTML, you would do this: `` – Ethan Brown Jan 30 '14 at 20:02
  • ok thanks! If you don't mind me asking though, how does it know to use that as the polyfill? I mean its never called to in the program. – Joshua Feb 03 '14 at 17:09
  • Basically, the script checks for the existence of `Windows.btoa`. If it's present, it doesn't do anything. If it's not, it creates the method. – Ethan Brown Feb 03 '14 at 17:15
  • Ok thanks. I know when I just tried it, it showed the page trying to load but wouldn't bring up the download for the table. I guess I messed something else up somewhere. – Joshua Feb 03 '14 at 17:44
1

add this line on the header of your page, and it will be fixed

<meta http-equiv="X-UA-Compatible" content="IE=edge">
stoneskin
  • 297
  • 5
  • 5