1

I've got this Knockout custom binding, all alone in one file (allowing to bind to a jQuery widget):

(function ($, ko, undefined) {
    ko.bindingHandlers.switch = {
        init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            $(element).switch(valueAccessor());
        }
    };
}(jQuery, ko));

The project This file is downloaded at some point using the jQuery._evalUrl function. I could describe the whole process, but I'm not sure it would be relevant.

The problem is that in IE8, I get the good old error 80020101 during the $.ajax from the function.

I looked around and found that this error can happen for almost any reason ( see these questions that sum up some of them).

I didn't find any JS error in the file (but I'm not a JS guru), and eventually looked at the jQuery._evalUrl function. Here is the source in jQuery 1.11.1:

jQuery._evalUrl = function( url ) {
    return jQuery.ajax({
        url: url,
        type: "GET",
        dataType: "script",
        async: false,
        global: false,
        "throws": true
    });
};

Out of despair, I changed

dataType: "script",

to

dataType: "text/javascript",

and... that worked.

But in fact, writing

dataType: "dummy",

worked as well. But removing the entire dataType line didn't.

I'm not sure I get why. Is the code evaluated when dataType="script" and not otherwise ?

So I tried to rewrite the custom binding step by step, testing all the way down, to track a potential code problem using the original dataType: "script".

This doesn't raise an error:

(function ($, ko, undefined) {
}(jQuery, ko));

This does:

(function ($, ko, undefined) {
    ko.bindingHandlers.switch = {
    };
}(jQuery, ko));

In fact, those two also fail:

(function ($, ko, undefined) {
    ko.bindingHandlers.switch = 1;

}(jQuery, ko));

(function ($, ko, undefined) {
    ko.bindingHandlers.switch = "a";
}(jQuery, ko));

Any ideas what the hell could be happening ?

Community
  • 1
  • 1
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
  • Evaluating as script returns plain text - "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests. - was this the intetion – PW Kad Oct 01 '14 at 16:24
  • OMG, I get it. The SO syntax highlighter shows me that I missed something obvious: `switch` is a reserved word (yeah, right). And IE8 doesn't like that, where other don't mind... – xlecoustillier Oct 01 '14 at 16:29
  • That's awesome the first case of the code highlighter helping someone solve their own problem :) – PW Kad Oct 01 '14 at 16:38

0 Answers0