McKayla
McKayla

Reputation: 6959

Javascript: Seemingly valid code causing Uncaught Error: NOT_SUPPORTED_ERR: DOM Exception 9 on line 3

var returned = values.make(function (value, index) {
    return items[index].value = value;
});

I have the above snippet.

Values is an array of values to be assigned to different elements.

Make is essentially the equivalent of Array.prototype.map.

Array.prototype.make = function (loop, playground) {
    var loop = loop || function (value) { return value },
        playground = playground || this;

    if (loop.Type !== "Function") throw "Loop [0] is not a function.";

    var returned = [];
    for (var i = 0; i < this.length; i++)
        returned[i] = loop.apply(playground, [this[i], i, this]);

    return returned;
};

Also, I have Function.prototype.Type = "Function"; in the same file, so it's not .Type throwing an error. .Type works perfectly.

Along with Function, these also have .Type's.

Object.prototype.Type = "Object";
Array.prototype.Type = "Array";
RegExp.prototype.Type = "RegExp";
String.prototype.Type = "String";
Number.prototype.Type = "Number";
Boolean.prototype.Type = "Boolean";
XMLHttpRequest.prototype.Type = "XMLHttpRequest";
Date.prototype.Type = "Date";

Items is the array of different elements.

[<input type="text" />, <input type="text" />, <input type="text" />]

I keep getting this error.

Uncaught Error: NOT_SUPPORTED_ERR: DOM Exception 9 on line 3

I get that error and it doesn't make any sense, because there isn't any code on that line.

I'm at a total loss.

Does anyone notice anything wrong with that code?

Update: I don't know what happened, but I fixed it.

Since no one gave the correct answer, I'll just give it to the only guy who tried.

*clap clap clap*

Upvotes: 5

Views: 4208

Answers (2)

Simon27
Simon27

Reputation: 176

I was having the same issue in a different scenario, but using the latest version of Prototype (1.7 instead of 1.5) fixed it.

Upvotes: -1

Vivin Paliath
Vivin Paliath

Reputation: 95598

Can you post a small example that reproduces this error?

Other than that, there are a few errors in your Javascript:

I added a semicolon here:

var loop = loop || function (value) { return value; },
   playground = playground || this;

Although semicolons are not necessary, I like to use them because otherwise you can be bitten by subtle errors.

And, you need to use typeof not .Type:

if (typeof loop !== "function") throw "Loop [0] is not a function.";

Also, if items is just an array of strings as you have, then items[index].value doesn't make sense, since strings don't have a value property. This part looks particularly suspicious to me. Although I didn't get the same error you did when I left that bit in, I think it would merit closer examination.

You mentioned that you're using a 3rd-party library so the part about typeof doesn't matter. You also mentioned that you were using actual input elements in your array so the second part doesn't matter either.

I tried out your code again, this time creating input elements with document.createElement:

Array.prototype.make = function (loop, playground) {
    var loop = loop || function (value) { return value; },
        playground = playground || this;

    if (typeof loop !== "function") throw "Loop [0] is not a function.";

    var returned = [];
    for (var i = 0; i < this.length; i++)
        returned[i] = loop.apply(playground, [this[i], i, this]);

    return returned;
};

var items = [];
items.push(document.createElement("INPUT"));
items.push(document.createElement("INPUT"));
items.push(document.createElement("INPUT"));

var values = [4, 5, 6];

var returned = values.make(function (value, index) {
    return items[index].value = value;
});

console.log(items[0].value, items[1].value, items[2].value);

//firebug shows: 4 5 6

So it appears that your code is working by itself. Can you try this code out by itself on a fresh page? That way you can verify if it is something on the original page that is interacting with your code and causing this error.

Upvotes: 5

Related Questions