Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code not running in IE 11, works fine in Chrome

People also ask

Why JavaScript is not working in Internet Explorer?

Internet Explorer When the "Internet Options" window opens, select the Security tab. On the "Security" tab, make sure the Internet zone is selected, and then click on the "Custom level..." button. In the Security Settings – Internet Zone dialog box, click Enable for Active Scripting in the Scripting section.

How do I test IE11 in Chrome?

Test IE allows you to preview and test websites in all versions of Edge and Internet Explorer (IE6 - IE11). Just click on the extension from the Chrome toolbar, select the Internet Explorer version of your choice, and jumpstart a test session in Browser Live.

How do I view JavaScript errors in IE 11?

Load you website, then press f12 to display the Developer tool.. any errors that have occurred will be listed in the Developer tool console. Display the Developer tool, selecting the Debug tab, select an option on the dropdown on that tab, 'Break on all exceptions' or 'Break on unhandled exceptions'.

Why is my JavaScript code not working in Chrome?

Google ChromeIn the "Settings" section click on the "Show advanced settings..." Under the the "Privacy" click on the "Content settings...". When the dialog window opens, look for the "JavaScript" section and select "Allow all sites to run JavaScript (recommended)". Click on the "OK" button to close it.


String.prototype.startsWith is a standard method in the most recent version of JavaScript, ES6.

Looking at the compatibility table below, we can see that it is supported on all current major platforms, except versions of Internet Explorer.

╔═══════════════╦════════╦═════════╦═══════╦═══════════════════╦═══════╦════════╗
║    Feature    ║ Chrome ║ Firefox ║ Edge  ║ Internet Explorer ║ Opera ║ Safari ║
╠═══════════════╬════════╬═════════╬═══════╬═══════════════════╬═══════╬════════╣
║ Basic Support ║    41+ ║     17+ ║ (Yes) ║ No Support        ║    28 ║      9 ║
╚═══════════════╩════════╩═════════╩═══════╩═══════════════════╩═══════╩════════╝

You'll need to implement .startsWith yourself. Here is the polyfill:

if (!String.prototype.startsWith) {
  String.prototype.startsWith = function(searchString, position) {
    position = position || 0;
    return this.indexOf(searchString, position) === position;
  };
}

text.indexOf("newString") is the best method instead of startsWith.

Example:

var text = "Format";
if(text.indexOf("Format") == 0) {
    alert(text + " = Format");
} else {
    alert(text + " != Format");
}

If this is happening in Angular 2+ application, you can just uncomment string polyfills in polyfills.ts:

import 'core-js/es6/string';

Replace the startsWith function with:

yourString.indexOf(searchString, position) // where position can be set to 0

This will support all browsers including IE

Position can be set to 0 for string matching from the start meaning 0th position.