3

It seems that for some question pages, I receive the error TypeError: i is not a function in the console.

For example for the question what can you do when window.location.href returns null, the error is pointing to line 1398, column 463 of the page. That line is a <script> block that contains this minified code, that I beautified here:

window.clc_request = 'AoZ7GBgpv9QIUv1ZAF02UwICAAAAAgAAAANAAAAAfGphdmFzY3JpcHR8eC11c2VyLXJlZ2lzdGVyZWR8eC01MDAwcGx1cy1yZXB8eC1wZXJzb25hLWZyb250ZW5kfACRZGCvJBE00Zr8';;
! function i(n) {
  var u, r;
  try {
    if ("function" != typeof [].indexOf) return;
    if (!window.addEventListener) return;
    if (!n.allowed) return;
    if (!RegExp(n.allowed).test(location.hostname)) return;
    var t = window,
      i = t.document,
      f = n.wv;
    if (i.hidden && f) return void i.addEventListener("visibilitychange", function t() {
      i.hidden || (i(n), i.removeEventListener("visibilitychange", t))
    });
    if (t.clc = t.clc || {}, t.clc.init = t.clc.init || [], n.autoload && t.clc.init.push(function(n) {
        return n.load()
      }), t.clc.options) {
      for (u in t.clc.options) n[u] = t.clc.options[u];
      t.clc.options = n
    } else t.clc.options = n;
    r = i.createElement("script");
    r.async = !0;
    r.src = n.lib;
    i.body.appendChild(r)
  } catch (i) {}
}.call(null, {
  "lib": "https://cdn.sstatic.net/clc/clc.min.js?v=d80e98d967bf",
  "u": "https://clc.stackoverflow.com/markup.js",
  "azw": true,
  "kt": 2000,
  "tto": true,
  "d": {
    "sb": ["hireme", "clc-sb"],
    "tlb": ["careers1", "clc-tlb"],
    "mlb": ["careers3", "clc-mlb"]
  },
  "h": "clc.stackoverflow.com",
  "autoload": true,
  "allowed": "^(((talent\\.)?stackoverflow)|(blog\\.codinghorror)|(serverfault|askubuntu)|([^\\.]+\\.stackexchange))\\.com$",
  "wv": true
});

The stack trace includes the function t, which is the visibilitychange event listener. Indeed, whenever I switch to a different tab, then switch back, the error counter increases by one.

The outer function is already called i, but i is also used as a local variable inside it that window.document is assigned to (worryingly, t is also used as both window and a function). The event listener tries to use i as both, by checking its hidden property, but also calling it as a function; but it’s not a function.

This script block appears after some ad-related markup, so I assume, this is another ad-related script.

1
  • 3
    This is probably due to an upgrade of UglifyJS from v2 to v3. I've been able to reproduce this, working on a fix now.
    – rossipedia
    Jun 29, 2017 at 20:30

1 Answer 1

5

Ok this was a fun one.

TL;DR

Our minifier was broken. It was weak and foolish, so I destroyed it.

Ok, no not really. But we did have some legacy minification code that wasn't working quite right and created a naming collision.

TS;WM (too short; want more)

Essentially what was happening is that we (the Ad Server team) were running our loader script (which is what's embedded on Stack Overflow) through uglify-js during the build process. This removes things like console.log statements, strips comments, and all that other standard minify stuff. Including (and this is the important bit) name mangling.

However, that particular piece of code also was getting run through a server-side minification process (via AjaxMin) which had an unfortunate bug: while renaming variables, it apparently doesn't check for collisions with existing function names that already look minified.

In this case, the load() function was renamed to i() via uglify-js, and then AjaxMin renamed another variable to i, and voila: name collision.

Super edge case, I know. The fix was basically to restructure how we serve this bit of code, and it no longer uses AjaxMin.

Anywho, thanks for the tip, this went under our radar for a while (about 9 days), but it should be fixed now!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .