Troels
Troels

Reputation: 57

Javascript global object calls function?

I have a very odd problem with javascript. My code is rather long so here is an example of the structure and the problem:

var x = new function f() {
    this.id = "";
}

function g(obj) {
    if (x.id == "") {
        ...
        obj.firstChild.setAttribute("onclick", "javascript:o();");
        ...
        x.id = obj.id;
    } else if (x.id != obj.id) {
        ...
        x.id = "";
        g(obj);
    }
}

function o() {...
    if (something == something) {
        ...
    } else {
        ...
        x.id = ""; // if-statement of the g() function is called here?
    }
}

As you can see, the if-statement of the g() function is for some reason called or re-run upon x.id being changed. I simply cannot understand this, because they are not in the same scope, and changing a variable should under no circumstances trigger anything?

Any help would be greatly appreciated.

Upvotes: 1

Views: 473

Answers (2)

Troels
Troels

Reputation: 57

Thank you very much for your answers. I have now figured out the problem. The function g() is called whenever a certain tableRow object is clicked. This same tableRow contains the obj.firstChild. Because of this, when i click on obj.firstChild, both o() and g() is executed.

Now i have got to figure out how i can prevent g() from being called when obj.firsChild is clicked.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074148

x.id = ""; // if-statement of the g() function is called here?

If the code really is as you show it, that line of code will not generate a function call. The line is simply an assignment to the id property of x. The only way a function call could be triggered by your assigning to the id property of x would be if you were using a browser that supported property accessors (getters and setters), which is very unlikely (and there's nothing in your code doing so).

Something else would appear to be calling g in a loop of some kind, and so your changing the value makes the next call to g see the change. As Jonathon pointed out in his comment on the question, this could be something you've set up with setTimeout, or setInterval, or in an even handler (on mouse move, for instance), etc., but it's nothing in the quoted code.


Possibly off-topic:

You have this line of code at the outset:

var x = new function f() {
    this.id = "";
}

That code uses a "named function expression," which should be valid but causes problems in some implemenations (IE, for instance). Split it up to get the desired result reliably cross-browser:

var x = new f();
function f() {
    this.id = "";
}

Off-topic:

You don't use the javascript: pseudo-protocol except on attributes that take a URI (like href on a elements). The onclick attribute does not take a URI, it takes JavaScript code, so change:

obj.firstChild.setAttribute("onclick", "javascript:o();");

to

obj.firstChild.setAttribute("onclick", "o();");

But, better yet, assign the handler directly:

obj.firstChild.onclick = o;

...or even consider using addEventListener or its IE-counterpart attachEvent instead.

Upvotes: 3

Related Questions