Ralf Strauss
Ralf Strauss

Reputation: 57

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?

Upvotes: 3

Views: 884

Answers (2)

Drag13
Drag13

Reputation: 5988

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.

Upvotes: 4

Quentin
Quentin

Reputation: 943510

Internet Explorer 11 is too old to support padStart.

You should have seen an error message about it being undefined when you looked at the developer tools when you debugged your code in IE. You did debug it, didn't you?

It is a legacy browser that Microsoft only provides for the benefit of out-of-date intranet based web applications.

MDN links to a couple of polyfills if you really need to support it in new development. As a rule of thumb, you should be working to eliminate it from your organisation instead.

Upvotes: 3

Related Questions