I find the code below , but I can't understand this.
if (!-[1, ] && !window.XMLHttpRequest) {
document.execCommand("BackgroundImageCache", false, true);
}
What does if(!-[1,]) mean ? Thanks
It's a hack to detect old Internet Explorer. -[1,] is -1 in modern browsers (so false with !) but NaN in old IE (true negated). The first version to return correct result is IE9.
That exact code will never yield anything other than false, so it is nonsensical as entered. I assume that this is rendered output and that depending on some serverside variable, it may sometimes be something different.
Seeing as it uses window.XMLHttpRequest, I realize it could also be some poor form of browser check. [1,] creates an array, but the trailing comma will make the array treated differently in Chrome and Internet Explorer. Chrome will recognize this as an array of only one number, which could be implicitly cast to a number, whereas IE will consider it to be an array containing two items, which can't be cast to a number.
-[1,0] will yield NaN in all browsers. -[1] will yield -1 in all browsers. Hence -[1,] will yield NaN in IE (and hence execute the code), and -1 in other browsers (and not execute the code).
This is a terrible hack. Don't use it.
If you want to find out whether window.XMLHttpRequest will work, test for that specifically, and not for anything else.
This is particularly bad code:
-[1, ] Results to the number -1.
!-[1, ] would always be false.
Since the [] implies an array, this is bad, cause if you had more than one value in that array, the - would give you a NaN.
Related
The following code is not mine. Created by Microsoft Azure for window.appInsights. This code works in Edge & IE, but fails in Chrome (throws "SyntaxError: expected expression, got '&&'" in the console). Firefox complains about it, but works anyway.
Can someone explain to me what this is doing (attempting to do)? I've not seen something like this before.
if (true === && "" !== "") {
UPDATE
Thank you all for your answers. I thought it was poorly written code, but wanted to be sure it didn't contain a coding shorthand I wasn't aware of. Your answers cleared that up for me !
It probably is a mistake . It shouldn't work. === is a binary operator and therefore expects 2 operands not 1. && is not an expression neither a valid identifier.
Read it in english. if true is strictly equal to or empty string is strictly not equal to empty string
i wonder what "" !== "" is trying to do since it will always evaluate to false lol
Note to people who pretend to be Moderators:
Do not dislike if you could not understand this question. It has enough information enough details and very useful to people who use font-icons in JS
I am trying to compare two strings that contain font-icons, but it is failed. I have tried comparing like below
a === b
a == b // Though Browser will do my conversion job
btoa(a) == btoa(b) // Browser scolded me.
Are there any alternative ways?
Fiddle
In your fiddle, element.innerText is returning undefined, hence it is failing.
Use element.textContent instead. Also, since your div contains , you'll need to take some extra precaution in some browsers since it might try and convert it into an object of some sort. (Currently happening on my browser)
I'am a javascript newbie, here is code from ExtJS which confuses me:
supportsSort = (function() {
var a = [1,2,3,4,5].sort(function(){ return 0; });
return a[0] === 1 && a[1] === 2 && a[2] === 3 && a[3] === 4 && a[4] === 5;
}()),
Is someone can tell me why ExtJS want to do this test?
It is better to attach some examples code.
Hesitant to post this as the answer as I'm admittedly just taking an educated guess but according to MDN, the browser-compatibility for Array.sort is listed as ECMAScript5 and "yes" for everything (as opposed to listing actual version numbers) - leaving a test for actual support more or less redundant.
The variable name is probably a little bit miss-leading though because if you actually follow what it's doing, the function that is passed to sort is just returning 0; typically you might return 1 or -1 depending on your comparison conditions in order to manipulate the order of the array - so by doing this the expected result is that the order of the array remains unchanged.
The return statement is just a chain of boolean checks as to whether the array is still in the same order as it was initially. Arguably then this supportsSort flag is there to check whether or not the browser/Javascript's implementation of the sort function is in fact a stable algorithm.
I think this test is needed to check if browser supports this feature or not.
As this is discussed in this post or in this link you can find Array.prototype.sort([comparator]). There is shown that not all browsers versions supports sort function.
These days there isn't browser which does not support this function (i'm talking about latests versions). But in case if Ext Js is used to develop for earlier versions it becomes needed.
I have an element that is dynamically created with new Element('div') and is then faded in, moved, and faded out. In every browser, my code works as expected – every browser but IE.
Internet Explorer complains that 'undefined' is null or not an object exactly as documented on Prototype's Lighthouse.
To get the error, I write, new Message('Your contact information has been saved');
The source for my Message class is available at this gist.
“Undefined is null of not an object” is a very basic error message that can be produced in a huge range of situations (it's JScript's effective equivalent of a null pointer exception). So it's unlikely yours is the same error as mentioned above.
I don't get that error though. I get “Invalid argument”, which seems to be caused by:
new Effect.Move(_div, {sync: true, x: '50%', y: 35, mode: 'relative' })
If I change the % to a normal integer pixel value it works fine. x is documented as accepting only an integer value not a CSS measurement; the '%' doesn't work for me in other browsers either (acts as 0). I guess you've made this feature up!
As Fabien mentioned, you also need to insert some var statements, otherwise you're scribbling over the globals and Message will blow up if you try to create two of them. Plus take care not to leave a trailing , in an array literal (due to the commented line), as this confuses IE.
I suppose it depends on how it's implemented. I'd love it if someone would come back and tell me "yes, in virtually all browsers, order of items will be changed only when necessary to satisfy the conditions of the sort.
What you're looking for is whether or not the algorithm is "stable". It is known that Firefox's is not, while IE's is. The javascript standard doesn't require a stable sorting algorithm.
Edit: Firefox 3+ has a stable sort. Please see http://www.hedgerwow.com/360/dhtml/js_array_stable_sort.html
Every browsers has different implemantation, so dont count on it.
I believe it depends on the type of objects you are sorting within your array. You can supply a "sort function" to array.sort() to determine the rules for sorting a particular object. For example, consider the function:
function sortInt(a, b){
if ( a < b )
return -1;
else if ( a == b )
return 0;
else if ( a > b )
return 1;
}
So this is obviously contrived but you can apply this same type of idea to any object that is "comparable". You will always return a -1, 0, or 1 depending if the a is less than, equal to, or greater than b (respectfully).
You would then say: array.sort(sortInt);
Caveat:
Forgive me if the syntax isn't perfect as I do not have an example on hand. I also am not sure about the stability of Array.sort() from a cross browser perspective.
Edit: Fixed the formatting for the psuedo code snippet
All the major browsers have stable sorting algorithms, and also (because there is some stupid code out there) they can handle inconsistent compare functions.