X
點擊此處開啟此網站的行動版。

技術支援討論區

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?

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?

額外的系統細節

這發生了

每當開啟 Firefox 的時候

問題發生於...

I upgraded to firefox 7

已安裝的外掛程式

  • Shockwave Flash 10.3 r183
  • NPRuntime Script Plug-in Library for Java(TM) Deploy
  • Next Generation Java Plug-in 1.6.0_26 for Mozilla browsers
  • Adobe PDF Plug-In For Firefox and Netscape
  • DRM Netscape Network Object
  • DRM Store Netscape Plugin
  • Npdsplay dll
  • Adobe Acrobat Plug-In Version 5.00 for Netscape

應用程式

  • User Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1

更多資訊

Tyler Breisacher 2 個解決方法 14 個答案

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;
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;

由 Tyler Breisacher 於 修改

提出問題者

Thanks a ton, MatrixFrog. Your suggested change seems to have fixed the issue.

Thanks a ton, MatrixFrog. Your suggested change seems to have fixed the issue.