Javascript function passed with an html element as an object (using "this" as the object being passed) does not obtain obj.offsetTop while works fine. Why?
I have recently upgraded to Firefox 7. In the earlier version that I had, all my pop-up images were showing up fine. For such pop-up images, I have a javascript function to which I pass the html object reference using "this" keyword.
For example, I would call the javascript function thus:
jsfnc(true, this, "some other text")
and in the function code, I would receive these parameters thus:
function jsfnc(status, objref, mytext) { ....
This jsfnc then in turn would call another function thus:
gettopleft(objref);
And in this gettopleft function, I would receive this objref parameter thus:
function gettopleft(obj) {
alert("In .js getTopLeft() function!"); var left = top = 0; if (obj.offsetParent) { do { left += obj.offsetLeft; top += obj.offsetTop; } while (obj = obj.offsetParent); } alert("In .js getTopLeft() function!\nLeft = "+left+"\nTop = "+top); return [top, left];
}
Now, in the firefox version earlier to my latest upgraded version 7, there used to be an indicator (in the form of a circle with a slash) in the address bar if a website's pop-ups have been blocked and whether I would like to allow such pop-ups. But in version 7, I see no such indicator. And during javascript execution, during debugging, I receive correct value for the
left += obj.offsetLeft;
but, I do not receive the correct value for
top += obj.offsetTop;
This is evident thru the alert box that I placed in my gettopleft function. This is what I see in the said alert box when this gettopleft function executes -
In .js TopLeft() function! Left = 1005 Top = [object Window]
As a result, when I don't have both the x, y cordinates (the top and left variables) values, my pop-up does not show.
So, what's happening in Firefox 7 that prevents my pop-up from showing?
All Replies (2)
I've run into this same problem (using the same snippet from quirksmode!), though Firefox 7 treats it differently than previous versions. The problem is here:
var left = top = 0;
Since you didn't say 'var top', you're referring to the *global* variable top (which is a reference to the window). In older versions of Firefox, this was allowed, it just meant you would be redefining that global variable, which is not what you intended. In Firefox 7, this is not allowed. You should see some sort of warning/error in the error console. I recommend the Console2 addon to make it easier to sort through errors. Anyway, the fix is simple:
var left = 0; var top = 0;
Modified
Thanks a ton, MatrixFrog. Your suggested change seems to have fixed the issue.