Array.prototype.find() vs IE11 - javascript

https://caniuse.com/#search=find states The find() method is not supported by IE11.
At the same time I'm testing this find() method in IE11 and I didn't find any traces of any wrong behavior.
I've also tested in IE11 the code
function isPrime(element, index, array) {
var start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) return false;
}
return (element > 1);
}
console.log([4, 5, 8, 12].find(isPrime)); // 5
from
SO: Array.prototype.find() is undefined
Yes, in IE11 it returns the expected result of 5 instead of TypeError: undefined is not a function, as SO: Array.prototype.find() is undefined in 2014 stated.
So... Am I missing something and IE11 really doesn't work properly with Array.prototype.find , or the last updates of IE11 that were made a while ago (but later than the SO question above was discussed in 2014) became to support this method?
Is https://caniuse.com/#search=find correct when says IE11 doesn't support Array.prototype.find ? Any evidence?
Thank you.
UPD: here is the screen of my IE11:

Everything you have read is correct. There is something flawed about your tests. Perhaps you included a Polyfill that added the method in IE11.

You can try following steps to validate:
Open a blank tab in IE.
Open console in dev tools.
Enter following code: [1,2,3].find(function(n) { !!n; })
If above code throws error (which it should), you are using a polyfill. Hence your code does not break.
If it works, only explanation is that somehow, some update has added its definition. But this is very unlikely as MS has stopped support for it.
This is what I get:

Related

Get rid of FormData.entries() in IE11

I need to get rid or skip FormData.entries() in IE11. I have the code to check for IE 11 from here:
https://stackoverflow.com/a/22242528/1824579
var formData = new FormData();
...
if (!navigator.appVersion.indexOf('Trident/') > -1) { //is 29 in IE; -1 in Chrome
for (var pair of formData.entries()) { //error in IE11
...
}
}
So all I want to achieve is, that if the Browser is IE11 it should skip this section. By now I am not able to achieve this. In the console i only get this error shown up:
SCRIPT1004: Expected ';' Index(1094, 31) which is exactly after the word pair in this line: for (var pair of formData.entries()) {
I don't know why IE11 is coming so far, because a log or the result of navigator.appVersion.indexOf('Trident/') is 29 in IE11.
for...of is not supported in IE11. This is a syntax-level issue that can't be solved by feature detection. Your best bet is to transpile your source code with something like Babel, targeting IE11.
The issue might be with using the logical NOT operator. Checking "indexOf > -1" should do the trick or you may need to use an extra set of brackets:
if (!(navigator.appVersion.indexOf('Trident/') > -1))

Javascript ES6 TypeError in Safari & IE

I am working with a Javascript code written by someone else who used ES6 in a Wordpress site. It makes an Ajax call to show a data in DOM, which works for Chrome and Firefox but for some reason Safari gives following error in console:
TypeError: document.querySelectorAll(".js_zip-lookup__submit").forEach is not a function. (In 'document.querySelectorAll(".js_zip-lookup__submit").forEach(function(e){e.addEventListener("click",function(){displayResults(e.parentNode.querySelector(".zip-lookup__input").value)})})', 'document.querySelectorAll(".js_zip-lookup__submit").forEach' is undefined)
This is the function:
function finderInit(){
document.querySelectorAll('.js_zip-lookup__submit').forEach(function(button){
button.addEventListener('click', function(){
const zip = button.parentNode.querySelector('.zip-lookup__input').value;
displayResults(zip);
});
});
document.querySelectorAll('.zip-lookup').forEach(function(form){
form.addEventListener('submit', function(e){
e.preventDefault();
const zip = form.querySelector('.zip-lookup__input').value;
displayResults(zip);
})
});
}
And I can't quite tell why Safari would have an issue with this, while Chrome/FF doesn't even log any error about this particular part in the console and it works just fine. I know it should be some browser compatibility issue but haven't found much information so far.
document.querySelectorAll returns NodeList object, not an array. As it can be seen on MDN, NodeList has forEach method but it's not well-supported, that's why it works in recent Firefox and Chrome but not in other browsers (Safari):
Although NodeList is not an Array, it is possible to iterate on it using forEach(). Several older browsers have not implemented this method yet.
To iterate over node lists and other iterators in a compatible way, Array.from should be used (can be polyfilled in older browsers):
Array.from(document.querySelectorAll(".js_zip-lookup__submit")).forEach(...);
I tried many variations of Array.prototype, but the only thing that solved IE & Safari compatibility issue was inclusion of this polypill snippet below, solution found in this blog:
(function () {
if ( typeof NodeList.prototype.forEach === "function" ) return false;
NodeList.prototype.forEach = Array.prototype.forEach;
})();
As mentioned by #Estus, older versions of Safari do not implement .forEach on nodelist objects. However, Array.prototype.forEach is defined as generic in the ES5.1 specifiction:
The forEach function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether the forEach function can be applied successfully to a host object is implementation-dependent.
Hence a workable solution is to call Array.prototype.forEach on the nodelist and pass it the function you want to execute. As a cut down (and cheap) test case:
var col = document.getElementsByTagName("p");
//col.forEach( function (el) {document.write(el.id)});
Array.prototype.forEach.call(col, function (el) {document.write(el.id)});
<p id="p1"></p>
<p id="p2"></p>
<p id="p3"></p>
<p id="p4"></p>
This was tested and found to work in Safari 5.1.7 for Windows and under Internet. Explorer 9 emulation. The commented out line reproduces the error reported in the post in Safarai 5.1.7.

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(' ');

String contains not working in IE

I am trying to use contains to find out if a phrase appears within a string
The code below works fine in FF and Chrome, however IE8-10 return an error.
SCRIPT438: Object doesn't support property or method 'contains'
var str = "This is a string";
if(str.contains("string")){
alert('Yes'};
}
Not sure why IE is throwing a error so any help would be much appreciated.
The .contains() function is an ES2015 feature that older Internet Explorer versions don't support.
The MDN page has a polyfill:
if ( !String.prototype.contains ) {
String.prototype.contains = function() {
return String.prototype.indexOf.apply( this, arguments ) !== -1;
};
}
A general guide for questions like this: type MDN something into the Google search box. If you don't find a result, then "something" probably doesn't exist in the JavaScript universe. If you do, then there's a pretty good chance that you'll find the answer you seek there.

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