Search code examples
javascriptinternet-explorer-11

Scripts won't run on internet explorer 11 (ie11)


I have a script below

var dt = new Date();
year  = dt.getFullYear();
month = (dt.getMonth() + 1).toString().padStart(2, "0");
day   = dt.getDate().toString().padStart(2, "0");

document.getElementById("date").innerHTML = year+"."+month+"."+day;

It's a simple script that displays current date (year, month and day).

The problem is that for some reason it won't work on internet explorer 11. (I have javascript enabled). On browsers like mozilla, chrome it works fine. Maybe the script should be differently written just for ie11?


Solution

  • The issue is that you are using .padStart method that is not supported by the IE

    You should check can i use before dealing with IE :)

    As an option, you can use the polyfill from MDN or from here. And here is EN version for the padStart

    In case you have to support IE in production, I would advise you to check transpilers (babel as example) It allows you to use modern code without worrying about old browsers. The downside is that configuration might be a bit tricky. But it really worth it.