Underscore js syntax error in IE 11 - javascript

I am using the underscore js library (http://underscorejs.org/#filter) for functionality in my app.
Everything works as expected in chrome. However when I run the same code in IE11 I get an js error in the console
SCRIPT1002: Syntax error
File: OptionSrv.js, Line: 197, Column: 62
When I clicked the file to bring me to the error the cursor is placed on the => - is this a red herring or should there be another way of doing this which works in both chrome and IE?
Note if I comment out the line in IE I don't get the console error however this obviously isn't the fix that I need
var group = myOptions.filter(g => g.options.indexOf(option.OptionCode) > -1);

Internet Explorer 11 does not support arrow functions.
That's the g => g.options.indexOf(option.OptionCode) > -1 part of your code.
You can use a normal anonymous (or named) function instead here, and it should work fine:
var group = myOptions.filter(function(g) {
g.options.indexOf(option.OptionCode) > -1);
});

IE11 does not support ES6 syntax. If you want to write ES6 syntax like Arrow functions, you can run your code through a transpiler like Babel.
If you like your client-side code to be compatible with older browsers and you don't care about new syntax, simply use ES5 syntax :)

Related

function default value input

i'm testing my website and everything is worked as wished, but when i work on ipad using safari i got this error:
Unexpected token '='. Expected a ')' or a ',' after a parameter declaration
This the line of my code where give me the error:
function searchLente(side = null) {
The problem is that when i test my website on a desktop is working (chrome, safari...) But not on ipad...
PD: My ipad is updated at last version.
Default values are a ES6 thingy, and most browsers don't support them.
Change it to
function searchLente(side) {
side = side === (void 0) ? null : side;
void 0 is a way to tell undefined. This code will check if size is undefined and then set size to a default value (null) if is true.
NOTE: This is what typescript actually does when transpiling to ES5 (which don't has defaults).
NOTE2: Don't think that your users will have ES6 enabled, as there are still browsers which don't support it. Use Babel if you want to write ES6 code and make it work for most browsers, or simply don't write ES6 code.

Unexpected token: operator (>) when trying to minify the javascript?

I'm trying to minify my javascript code using an online tool but everytime I try to do that I get this error:
// Error : Unexpected token: operator (>)
// Line : 1
// Col : 41
and this is on line 1:
var result = parsedObject.filter( audio => audio.filename === ''+audioFile+'' );
Could someone please advice on this issue and how to resolve it?
Thanks in advance.
Apparently, your minifier doesn't understand arrow functions, or it needs some option to be set to know you're doing ES2015+ ("ES6+") stuff. Your options are:
If it has an option, turn the option on; or
(you've now told us that you tried both https://jscompress.com/ and https://javascript-minifier.com/. jscompress.com has an "ECMAScript 2018 (via Babili)" tickbox in the upper right-hand corner that, when ticked, minifies your example code. I didn't find an option on javascript-minifier.com.)
If it doesn't, switch to a mninifier that does understand them; or
Don't use arrow functions. In this particular case that would look like:
var result = parsedObject.filter(function(audio) {
return audio.filename === ''+audioFile+'';
});
Use arrow function, but turn them into non-arrows before minifying by using a transpiler like Babel.
If you need to support any version of IE, you need to not send arrow functions to the browser (by using option 3 or 4 above). If you don't have to support IE, just modern browsers like Edge, Chrome, Firefox, and Safari, sending arrow functions to the browser is just fine.
Side note: You don't need those '' on either side of audioFile. If it's already a string, just remove them (=== audioFile). If it isn't already a string, just do one or the other, or use String(audioFile) to convert it, and do it once before the filter loop:
var audioFileString = String(audioFile); // or `'' + audioFile` or `audioFile + ''`
var result = parsedObject.filter(function(audio) {
return audio.filename === audioFileString;
});
The tool you are using does not support arrow functions (which are a relatively new feature).
You can:
Find a minifying tool which supports modern JS
Not use arrow functions in the first place
Use a tool to transform your JS to ES5 before minifying it

For ..in loop unexpected ;

I have the following for loop in JS and it works in all browsers but I am getting an "Expected ; " error in IE11 for the for loop line. Do I have to change format to make it play nice?
for (var filter of filters) {
//do something
}
for...of is an ES6 feature and therefore unsupported in IE. You need to either translate this to an ordinary for loop, or transpile your code before running it in an old browser like IE.

How to collect only unique values in an Array in Internet Explorer 11 by JavaScript?

from many articles I have chosen this syntax to make unique values in an array.
pairs = pre_final_pairs.filter((elem, index) => pre_final_pairs.indexOf(elem) === index).join(' ');
This works perfectly in all browsers except Internet Explorer 11.
I have tried to find which of the command from the line is not compatible and I found that maybe the indexOf. But even if I tried to apply "fix" referred in How to fix Array indexOf() in JavaScript for Internet Explorer browsers still the page is not working in the IE11.
Also I have loaded https://code.google.com/archive/p/ddr-ecma5/ library in order to ensure that the ECMA commands will work.
And still getting SCRIPT1002: Syntax error
Do you see there a wrong part in the command?
Internet Explorer does support indexOf, but does not support arrow functions.
You can easily fix that using a regular function for the callback instead:
pairs = pre_final_pairs.filter(
function (elem, index) {
return pre_final_pairs.indexOf(elem) === index;
}
).join(' ');

Javascript error in IE8: Not implemented

This one really puzzles me, as the code looks completely harmless.
IE8 halts script execution with a message:
Not implemented. map.js line:66 char:5
Here is a snip from the code:
63 if(data.map[x] !== undefined && data.map[x][y] !== undefined) {
64
65 left = (x - data.dim.x_min)*32 + 30;
66 top = (data.dim.y_max - y)*32 + 30;
67
68 /* do stuff */
XX }
debug info: x:263 data.dim.x_min:263 y:172 data.dim.y_max:174
Data is object returned from JQuery Ajax call. This works in Firefox 3.0 and 3.5, safari 4.0.2 and I've only found this error when viewing the page in IE8. Forcing IE8 into IE7 mode does not make the error go away.
I don't have IE7 to debug with, but I got a tester saying that it doesn't work in IE7 either.
The variable 'top' used in the code is an object of type DispHTMLWindow2 (outermost window object) and already in use by the browsers and that is causing the conflict, as that object cant be the target of the assignment operation. It seems that Firefox and Safari ignore this, while IE does not allow scripts to overwrite this.
Solutions for this:
1) Declare top you are using as local variable to define it's scope where it is used.
2) Rename the variable to something that doesn't conflict with this predefined global.
Description of other variable names you shouldn't use
IE 8 has a great javascript debugger. You might want to add a breakpoint somewhere before the error and step through the code to see if something is strange with the data. IE8 is picky with trailing commas in lists which might be why you only get the error in it. You can pull the debugger up with F12, click Script and choose start debugging. You can add a break point by clicking on the margin where the line numbers are.

Categories

Resources