Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Date' is undefined in IE9 in javascript loaded by FacePile

I'm currently getting an error within Facebook's FacePile code, and I'm baffled by the cause.

facepile.php loads a script which, among other things, has these lines (when pretty-printed):

...
o = document.createElement('script');
o.src = l[n];
o.async = true;
o.onload = h;
o.onreadystatechange = function() {
  if (o.readyState in c) {
    h();
    o.onreadystatechange = null;
  }
};
d++;
a.appendChild(o);
...

(a == document.body, d++ is irrelevant here)

This code loads a script with src = http://static.ak.fbcdn.net/rsrc.php/v1/yW/r/pmR8u_Z_9_0.js or something equally cryptic (the filename changes occasionally).

In that script, there are these lines at the very top (also when pretty-printed):

 /*1331654128,176820664*/

if (window.CavalryLogger) {
  CavalryLogger.start_js(["\/8f24"]);
}

window.__DEV__ = window.__DEV__ || 0;
if (!window.skipDomainLower && document.domain.toLowerCase().match(/(^|\.)facebook\..*/))
  document.domain = window.location.hostname.replace(/^.*(facebook\..*)$/i, '$1');
function bagofholding() {
}
function bagof(a) {
  return function() {
    return a;
  };
}
if (!Date.now)
  Date.now = function now() {
    return new Date().getTime();
  };
if (!Array.isArray)
  Array.isArray = function(a) {
    return Object.prototype.toString.call(a) == '[object Array]';
  };
...

And I'm getting an error which says "SCRIPT5009: 'Date' is undefined", right at the if (!Date.now) portion. Debugging near that point reveals that Date, Array, Object, Function, etc are all undefined.

Er... how? window exists, as does document (though document.body is null) and a handful of others, but plenty of pre-defined objects aren't. Earlier versions of IE don't seem to have this problem, nor do any other browsers, but multiple machines running IE9 (including a clean VM) all have the same issue.

I doubt I can do anything about it, but I'm very curious how this is happening / what the underlying problem is. Does anyone know, or can they point me to something that might help?

-- edit:

Prior to posting this question, I had found this site: http://www.guypo.com/technical/ies-premature-execution-problem/

While it seemed (and still does) like it might be the source of the problem, I can't replicate it under any smaller circumstances. All combinations I've tried still have Date, etc defined ; which isn't too surprising, as otherwise I'm sure others would be seeing many more problems with IE.

like image 997
Groxx Avatar asked Mar 14 '12 02:03

Groxx


People also ask

Why does an undefined variable return undefined in JavaScript?

An undefined variable or anything without a value will always return "undefined" in JavaScript. This is not the same as null, despite the fact that both imply an empty state. You'll typically assign a value to a variable after you declare it, but this is not always the case.

How to get the undefined primitive value in JavaScript?

The void operator is often used to obtain the undefined primitive value. You can do this using " void (0) " which is similar to " void 0 " as you can see below: In the actual sense, this works like direct comparison (which we saw before). But we would replace undefined with void (0) or void 0 as seen below:

Is there a datatype for an undefined value?

Luckily for us undefined is a datatype for an undefined value as you can see below: ‌ With this we can now use the datatype to check undefined for all types of data as we saw above. Here is what the check will look like for all three scenarios we have considered:


2 Answers

If you step through with a javascript debugger at the first point any JS gets run. At the same time add a watch for Date/Array etc. and note when it goes to null. Might be slow and laborious but I can't see why it wouldn't work.

like image 91
Lee Taylor Avatar answered Oct 22 '22 22:10

Lee Taylor


You may want to try adding the script in a document.ready function. In other words, insure that the FB script is processed only after the DOM is ready. But, based on the link you give to Guy's Pod (great article, by the way), it seems you're right in the assertion that IE is downloading and executing the script pre-maturely (hence my suggestion to add a wrapper so that it only executes after the DOM ready event). IE9 is probably sandboxing the executing script (outside the document/window scope).

like image 1
Joe Johnson Avatar answered Oct 22 '22 23:10

Joe Johnson