For ..in loop unexpected ; - javascript

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.

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

Underscore js syntax error in IE 11

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 :)

Object doesn't support this property or method 'eq' Internet Explorer only

This snippet of code...
sourcerow = $('#' + recordid);
item = sourcerow.find('td');
item.eq(1).text(itwcode);
Works fine in Firefox and Chrome.
But today I discovered that IE throws an error on this line - item.eq(1).text(itwcode);
The error is Object does not support this property or method 'eq'
I thought maybe 'item' was not properly being recognised as a jquery element so I tried this..
sourcerow = $('#' + recordid);
item = $(sourcerow.find('td'));
item.eq(1).text(itwcode);
Same error, so I tried this...
sourcerow = $('#' + recordid);
item = sourcerow.find('td');
$(item).eq(1).text(itwcode);
But now IE throws an error from inside the jquery library...
Invalid calling object jquery-1.9.1.min.js line 3 character 7849
Is there a way to solve or get around this error?
EDIT: As suggested by mikakun in the comments, I changed the name of the variable from 'item' to something else ('rowitem') and the code now works in IE. It's odd though because item is not a reserved word, and it worked fine in other browsers.
EDIT 2 Alternatively - leaving the name as 'item' but adding the 'var' keyword also fixed the code. item is used in another scope (but in a very similar way so same type)
In a practical sense, and if you use compilation for your project, you might consider to add at the top of your entry point
import 'core-js'
At the moment core-js polyfill library is the easiest way to make Cross Browser Support

"For in" loop doesn't iterate over screen object in IE

This works in all browsers:
for (var i in navigator) {
console.log(i, ':', navigator[i]);
}
But this doesn't work in IE 6 or 7: (loops zero times)
for (var i in screen) {
console.log(i, ':', screen[i]);
}
What's different about the screen object?
And more importantly, how would I go about looping through it in IE?
Demo: http://jsfiddle.net/Atepq/
What's different is that it's different...
MDN points out that screen is not actually part of any spec...
https://developer.mozilla.org/en/DOM/window.screen#Specification
When I run tests, IE9 lets you get all the properties off the screen object with:
for (var prop in screen) {
console.log(prop);
}
But, it won't let you access some of the properties to get their value. When you do so, it stops JS execution. It doesn't even throw an exception that you can catch.
Yes, this is brain dead, but it's not out of character with things that IE does, even in IE9.
If you really need to do this, you could one at a time figure out which properties IE doesn't work with and code to test for those and avoid them. It might have to be updated from version to version, but it could get you going for whatever you're trying to do.

Categories

Resources