function default value input - javascript

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.

Related

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

SCRIPT1014: Invalid Character Grave Accent (`) IE [duplicate]

Here's a new one, IE11 is throwing errors on code that works in every other browser.
Anyway, after a few hours of "fixing" code so that IE11 doesn't fall on its own face I have come across an error I just cannot seem to find a solution to. Here is the code in question:
$('input[name="messageAccount"]').change(function () {
$aButton.show();
var addedIds = $("#hdnfield").val();
if (addedIds == null || addedIds === "") {
$("#hdnfield").val(this.value);
} else {
$("#hdnfield").val(`${addedIds}${this.value}`);
};
});
This is nested on the inside of the success call in an Ajax request. IE's debugger is saying the error is coming from the content of the else statement but it's also reading all the brackets wrong. For example the opening parenthesis on this function is being "closed" after the closing bracket for the Ajax request... (hopefully that made sense).
Has anyone else had a similar issue with IE before? I've got a number of other bugs to fix so if there are any replies I will post back as soon as I can. Thanks in advance.
EDIT: just for reference I am currently running this locally and is part of what will become an internally hosted web app.
It's because "`" (template strings) is not supported by IE : http://caniuse.com/#search=string
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
See https://stackoverflow.com/a/27678299/2195875
To avoid this error you can either use a ES6 to ES5 transpiler, or just directly write valid ES5 javascript:
instead of
`string text ${expression} string text`
write
"string text "+expression+" string text"
You can also add polyfill to your html:
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.26.0/polyfill.min.js"/>

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

Old JavaScript Function Crashes Browser

I'm integrating a mootools script onto a page which has very old JavaScript functions which run a navigation vertical menu. This old script will be hard to change now.
The line breaking is:
function stgobj(id) {
with(document) return nIE && nVER < 5 ? all[id] : nNN4 ? layers[id] : getElementById(id);
}
Not sure exactly what's it's purpose, but it looks like it's rendering some elements. If commented the menu will disappear.
FF, Chrome, IE(doesn't crash, but menu does not render)
Any quick patch to resolve the browsers crashing?
Looks like its purpose is to return the element corresponding to the given ID. The code simply uses some different methods based on the browser - document.all for IE5 and earlier, and document.layers for Netscape 4. Unless you need to support those ancient browsers, you could alter the function to return just document.getElementById(id). Or better yet, ditch this function altogether and call document.getElementById directly.
However, if it's crashing modern browsers like Firefox and Chrome, then you should also look at the browser detection logic (the code that populates the nIE, nVER and nNN4 variables), otherwise it might just end up crashing elsewhere.
It's a "compatibility" function for document.getElementById. I think you should be able to equal it:
stgobj = document.getElementById.bind(document);

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