| Home | Forums | Reviews | Guides | Newsgroups | Register | Search |
![]() |
| Thread Tools |
| moha297 |
|
|
|
| |
|
Peter Michaux
Guest
Posts: n/a
|
On Dec 28, 11:32 am, moha297 <moha...@gmail.com> wrote:
> > I want to get the top and left values for a div on the screen. physical screen or upper-left corner of the page (which may be out of view if the page is scrolled.)? As Richard Cornford has mentioned here many times, this problem is not solved in general. If your div has parents that scroll, have table elements, is a button, etc, then the calculation of the div's upper- left corner relative to the upper-left corner of the page is complex. > I have been using the code to calculate the top and left values. > > var total1 = 0; > var total2 = 0; > while(element){ > total1+=element.offsetTop; > total2+=element.offsetLeft; > try{ > element=element.offsetParent; > > }catch(E){ > break; > } > } You should not need a try-catch block to calculate the position. Using feature detection once when this code first runs, or when the page first loads, should be enough to know how to calculate the offset in that browser for the remainder of the life of the page. Also, perhaps someone else can comment on the possible slowness of the try-catch. I rarely use them. > For the same DOM TREE this code is giving a performance reading of > 30msec in IE8 and 80 to 200msec in IE6. I want to gain a considerable > performance improvement in IE6. Why is 80 ms considered a performance problem? > I am open to all ideas. > > By the way I also tried > > var total1 = 0; > var total2 = 0; > do{ > total1+=element.offsetTop; > total2+=element.offsetLeft; > > }while(element=element.offsetParent); > > after reading a blog entry or two. The performace is similar. > > I found that the bulk of the time is always spent in getting property > value rather than parsing the DOM TREE which less constantly from my > logs.(I might be wrong) "parsing the DOM TREE"? That is done when the page loads, not when your calculation is occurring. > Also the code I have put here is on the fly....might have made > mistakes. The better the code you post, the more valuable the responses will be. Peter |
|
|
|
|
|||
|
|||
| Peter Michaux |
|
|
|
| |
|
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
|
moha297 wrote:
> I want to get the top and left values for a div on the screen. > > I have been using the code to calculate the top and left values. > > var total1 = 0; > var total2 = 0; > while(element){ > total1+=element.offsetTop; > total2+=element.offsetLeft; > try{ > element=element.offsetParent; > }catch(E){ Should be `e' as it does not refer to a constructor. > break; > } > } > > For the same DOM TREE this code is giving a performance reading of > 30msec in IE8 and 80 to 200msec in IE6. That is unsurprising since that try...catch statement does not do anything useful here as the assignment is not going to fail: If `element' does not refer to an object, the `while (element)' statement prevents execution from ever reaching the problematic assignment. If `element' does refer to an object, and that object does not have an `offsetParent' property or its `offsetParent' property value is `undefined' or `null', `element' will be assigned either `undefined' or `null'. Since that converts to `false', the next iteration is not going to happen and the case that one would attempt to access the `offsetParent' property of `undefined' or `null'. > I want to gain a considerable > performance improvement in IE6. > > I am open to all ideas. In most cases you do not need to determine the absolute position of an element in the first place. For example, if you want to move an element by 10 px towards the right margin and by 20 px towards the bottom margin, you simply increase its current `left' and `top' px-measured style property values by those distances (while keeping the unit). If you have not set those properties before, use the element's computed style. However, if you insist, read this article, among others about this oft- discussed problem in this newsgroup: <https://developer.mozilla.org/en/Determining_the_dimensions_of_elements> > By the way I also tried > > var total1 = 0; > var total2 = 0; > do{ > total1+=element.offsetTop; > total2+=element.offsetLeft; > }while(element=element.offsetParent); > > after reading a blog entry or two. Do not believe into anything written. > The performace is similar. It should be more efficient. Are you sure your benchmark is sound? > I found that the bulk of the time is always spent in getting property > value rather than parsing the DOM TREE which less constantly from my > logs.(I might be wrong) You are. You do not know what "(to) parse" means to begin with. > Also the code I have put here is on the fly....might have made > mistakes. It is hardly readable in the first place. > Am looking at performance gain. The floor is open to try anything only > constraint is just javascript, jquery, prototype etc are not an > option. jQuery and Prototype are written in "javascript", though. But you are correct avoiding them because of their bad code quality. PointedEars -- realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann |
|
|
|
|
|||
|
|||
| Thomas 'PointedEars' Lahn |
|
GTalbot
Guest
Posts: n/a
|
On 28 déc, 12:32, moha297 <moha...@gmail.com> wrote:
> I want to get the top and left values for a div on the screen. > As Peter Michaux replied to you, your description of the problem for which you require assistance is not accurate, too general. An URL would have helped. And maybe, just maybe, you may be wrongly using offsetLeft and offsetTop to get the left and top values of a div on the screen. We can't be sure of this without a real webpage, URL. > I have been using the code to calculate the top and left values. > > var total1 = 0; > var total2 = 0; > while(element){ > total1+=element.offsetTop; > total2+=element.offsetLeft; > try{ > element=element.offsetParent; > > }catch(E){ > break; > } > } First, the while statement does not make sense. You want an element (which is going to execute the controlled block) that has offsetTop and offsetLeft values to do the controlled block. Therefore, your while statement should be while (element.offsetParent) {..controlled block..} meaning as long as the current element being examined has an non-null offsetParent... Second, using a try.. catch does not perfection make sense from a debugging perspective and from a property detection support. Let's say the assignment fails because the current element being examined in that while block does not have an offsetParent: why should it generates an exception or an error object? Anyway, try.. catch is for managing exceptions, for debugging purposes. At least, this is what I would want to do when choosing to add a try...catch. And here, you do not even try to identify the error message, error line, type of error, etc.. So why resort to a try..catch block anyway? Third, your local variable identifiers (total1, total2) are not recommendable. You should always try to choose identifiers for variables that are meaningful, intuitive, that helps debugging, code maintenance, examining in debugging tools, that helps review by others who may not be accustomed to your internal function logic (or help review by yourself years later). This helps everyone and can make a huge difference when the code is very long, complex, with many intricated functions. Here's how I did the same function 7 years ago: var Element = evt.target ; var CalculatedTotalOffsetLeft = CalculatedTotalOffsetTop = 0 ; while (Element.offsetParent) { CalculatedTotalOffsetLeft += Element.offsetLeft ; CalculatedTotalOffsetTop += Element.offsetTop ; Element = Element.offsetParent ; } ; OffsetXForNS6 = evt.pageX - CalculatedTotalOffsetLeft ; OffsetYForNS6 = evt.pageY - CalculatedTotalOffsetTop ; http://www.gtalbot.org/DHTMLSection/...t#NoteOffsetXY http://www.gtalbot.org/DHTMLSection/...t#NoteOffsetXY > For the same DOM TREE this code is giving a performance reading of > 30msec in IE8 and 80 to 200msec in IE6. I want to gain a considerable > performance improvement in IE6. IE6 <sigh .. Why do you need to support IE6?>. Imagine that people are less and less using that browser and that IE8 implemented an improved offsetParent, offsetLeft and offsetTop model. With those numbers, don't you want to tell your IE6 users to upgrade or to switch? It would solve many many problems... "30msec in IE8 and 80 to 200msec" does not mean much if we do not know on which machine (CPU, RAM, video card, etc) these results are gathered from. 30msec is very long for a super-computer and 200msec is very fast on a Pentium 1 90Mhz. If nodeA is an area HTML element which has a map HTML element somewhere in the ancestor chain, then nodeA.offsetParent returns the nearest ancestor map HTML element... but that is not the case in IE 7; IE8 corrected this. http://www.gtalbot.org/BrowserBugsSe...tml#FourthTest If an element has no offsetParent, then its offsetLeft value must be 0 and its offsetTop value must be 0 ... but that is not the case in IE6. http://www.gtalbot.org/BrowserBugsSe...setValues.html > I am open to all ideas. Post an URL. Make sure your webpage is using valid markup code (including a doctype declaration, preferably declaring a strict DTD), uses valid CSS code. http://validator.w3.org/ http://jigsaw.w3.org/css-validator/ Also, read the comp.lang.javascript FAQ on posting code: http://www.jibbering.com/faq/#posting > By the way I also tried > > * * var total1 = 0; > * * var total2 = 0; > do{ > * total1+=element.offsetTop; > * * total2+=element.offsetLeft; > > }while(element=element.offsetParent); An assignment in a while clause is not recommendable, at least, definitely not my recommendation. Some assignment may succeed and return 0 (and be resolved as false while the assignment is successful and correct). Same thing with assignment in a if clause: if(a = b) may succeed but the value may be resolved as false because b == 0. Some other regular posters may help me here on this precise issue. > after reading a blog entry or two. The performace is similar. Why is performance important to you, with regards to offsetTop and offsetLeft and with regards to IE 6? Please elaborate. (There is such a thing has having an overexcessive number of positioned containers (like nested tables)... in a very bloated webpage) > I found that the bulk of the time is always spent in getting property > value rather than parsing the DOM TREE which less constantly from my > logs.(I might be wrong) Post an URL according to the constraints I gave you. And if you are open to all ideas, then add a IE6nomore or IE6RIP button in your webpage so that people get the message that IE6 is buggy, not recommendable, etc. should upgrade or switch. > Also the code I have put here is on the fly....might have made > mistakes. Well, whose fault is it then? Are you asking to get code answers on the fly as well... with possible mistakes too? " The better the code you post, the more valuable the responses will be. " Thank you Peter Michaud for speaking up my mind. Gérard |
|
|
|
|
|||
|
|||
| GTalbot |
|
GTalbot
Guest
Posts: n/a
|
On 28 déc, 12:32, moha297 <moha...@gmail.com> wrote:
> For the same DOM TREE this code is giving a performance reading of > 30msec in IE8 and 80 to 200msec in IE6. I want to gain a considerable > performance improvement in IE6. Those "30msec in IE8 and 80 to 200msec in IE6" numbers are entirely dependent and relative to the depth of the DOM tree of the tested webpage and to the CPU+RAM of the tested machines. The best possible (short term and long term) proactive solution for anyone/everyone involved is still to always use updated web-capable softwares and not to use very buggy, unreliable, non-trustworthy software like IE6. > I am open to all ideas. > > By the way I also tried > > * * var total1 = 0; > * * var total2 = 0; > do{ > * total1+=element.offsetTop; > * * total2+=element.offsetLeft; > > }while(element=element.offsetParent); Making an assignment in a while statement is not recommendable; definitely not my recommendation. The while statement should be a condition, an expression resulting into a boolean condition (and only that). The while statement should not be an assignment: you want to avoid side effects here. Correct and coherent is: do { total1+=element.offsetTop; total2+=element.offsetLeft; element = element.offsetParent; /* moves upward in the offsetParent containment hierarchy: this assignment must succeed because the while statement must have been true */ } while(element.offsetParent); /* first test if the current element being actually examined has an offsetParent before entering the controlled block */ Gérard |
|
|
|
|
|||
|
|||
| GTalbot |
|
David Mark
Guest
Posts: n/a
|
On Dec 28, 6:44*pm, GTalbot <newsgr...@gtalbot.org> wrote:
> On 28 déc, 12:32, moha297 <moha...@gmail.com> wrote: > > > I want to get the top and left values for a div on the screen. > > As Peter Michaux replied to you, your description of the problem for > which you require assistance is not accurate, too general. An URL > would have helped. And maybe, just maybe, you may be wrongly using > offsetLeft and offsetTop to get the left and top values of a div on > the screen. We can't be sure of this without a real webpage, URL. > > > I have been using the code to calculate the top and left values. > > > var total1 = 0; > > var total2 = 0; > > while(element){ > > total1+=element.offsetTop; > > total2+=element.offsetLeft; > > try{ > > element=element.offsetParent; > > > }catch(E){ > > break; > > } > > } > > First, the while statement does not make sense. > > You want an element (which is going to execute the controlled block) > that has offsetTop and offsetLeft values to do the controlled block. > Therefore, your while statement should be > > while (element.offsetParent) {..controlled block..} Just don't pass orphaned elements. I imagine that's what that mysterious try-catch was about. > meaning as long as the current element being examined has an non-null > offsetParent... And it better not have typeof "unknown" either. > > Second, using a try.. catch does not perfection make sense from a > debugging perspective and from a property detection support. Right. It's just hiding some other problem (code passing an orphaned element). > Let's say > the assignment fails because the current element being examined in > that while block does not have an offsetParent: why should it > generates an exception or an error object? It definitely can. > Anyway, try.. catch is for > managing exceptions, for debugging purposes. At least, this is what I > would want to do when choosing to add a try...catch. Right. There should not be a try-catch here. > And here, you do > not even try to identify the error message, error line, type of error, > etc.. So why resort to a try..catch block anyway? Exactly. It's just covering up a problem. > > Third, your local variable identifiers (total1, total2) are not > recommendable. You should always try to choose identifiers for > variables that are meaningful, intuitive, that helps debugging, code > maintenance, examining in debugging tools, that helps review by others > who may not be accustomed to your internal function logic (or help > review by yourself years later). This helps everyone and can make a > huge difference when the code is very long, complex, with many > intricated functions. > > Here's how I did the same function 7 years ago: > > var Element = evt.target ; > var CalculatedTotalOffsetLeft = CalculatedTotalOffsetTop = 0 ; while > (Element.offsetParent) > { > * CalculatedTotalOffsetLeft += Element.offsetLeft ; > * CalculatedTotalOffsetTop += Element.offsetTop ; > * Element = Element.offsetParent ; > > } ; > > OffsetXForNS6 = evt.pageX - CalculatedTotalOffsetLeft ; > OffsetYForNS6 = evt.pageY - CalculatedTotalOffsetTop ; > > http://www.gtalbot.org/DHTMLSection/...l#screenLeft#N... > > http://www.gtalbot.org/DHTMLSection/...l#screenLeft#N... That doesn't take borders into account. If you _must_ measure all the way to the document origin (virtually never), use getBoundingClientRect. > > > For the same DOM TREE this code is giving a performance reading of > > 30msec in IE8 and 80 to 200msec in IE6. I want to gain a considerable > > performance improvement in IE6. > > IE6 <sigh .. *Why do you need to support IE6?>. Because lots of corporate users are stuck with it? And because there's virtually no difference with IE7. And then there are the browsers that copied IE6 oddities... > Imagine that people > are less and less using that browser and that IE8 implemented an > improved offsetParent, offsetLeft and offsetTop model. Yes, less people are using IE6. IE6-8 (and perhaps earlier) all have getBoundingClientRect (and other browsers have copied). But it is much simpler and less problematic to measure to the origin of a positioned ancestor. > > With those numbers, don't you want to tell your IE6 users to upgrade > or to switch? It would solve many many problems... Because you can't tell users to upgrade their browsers. Some can't upgrade them. Some don't know what a browser _is_. > > "30msec in IE8 and 80 to 200msec" does not mean much if we do not know > on which machine (CPU, RAM, video card, etc) these results are > gathered from. 30msec is very long for a super-computer and 200msec is > very fast on a Pentium 1 90Mhz. > > If nodeA is an area HTML element which has a map HTML element > somewhere in the ancestor chain, then nodeA.offsetParent returns the > nearest ancestor map HTML element... but that is not the case in IE 7; > IE8 corrected this. > > http://www.gtalbot.org/BrowserBugsSe...OM-offsetParen... Skip the image maps and you'll be fine. [...] > > Why is performance important to you, with regards to offsetTop and > offsetLeft and with regards to IE 6? > Please elaborate. > > (There is such a thing has having an overexcessive number of > positioned containers (like nested tables)... in a very bloated > webpage) Yes. And there is such a thing as making one of them position:relative. > > > I found that the bulk of the time is always spent in getting property > > value rather than parsing the DOM TREE which less constantly from my > > logs.(I might be wrong) > > Post an URL according to the constraints I gave you. > > And if you are open to all ideas, then add a IE6nomore or IE6RIP > button in your webpage so that people get the message that IE6 is > buggy, not recommendable, etc. should upgrade or switch. Hell no. You _never_ insult the user's browser. They may not be able (or know how) to upgrade. |
|
|
|
|
|||
|
|||
| David Mark |
|
David Mark
Guest
Posts: n/a
|
On Dec 28, 1:01*pm, Peter Michaux <petermich...@gmail.com> wrote:
> On Dec 28, 11:32 am, moha297 <moha...@gmail.com> wrote: > > > > > I want to get the top and left values for a div on the screen. > > physical screen or upper-left corner of the page (which may be out of > view if the page is scrolled.)? > > As Richard Cornford has mentioned here many times, this problem is not > solved in general. If your div has parents that scroll, have table > elements, is a button, etc, then the calculation of the div's upper- > left corner relative to the upper-left corner of the page is complex. > It's been done. It's just not an advisable cross-browser design to rely on such complex code when simpler options are available. |
|
|
|
|
|||
|
|||
| David Mark |
|
GTalbot
Guest
Posts: n/a
|
On 28 déc, 19:33, David Mark <dmark.cins...@gmail.com> wrote:
> On Dec 28, 6:44*pm, GTalbot <newsgr...@gtalbot.org> wrote: > > > > > On 28 déc, 12:32, moha297 <moha...@gmail.com> wrote: > > > > I want to get the top and left values for a div on the screen. > > > As Peter Michaux replied to you, your description of the problem for > > which you require assistance is not accurate, too general. An URL > > would have helped. And maybe, just maybe, you may be wrongly using > > offsetLeft and offsetTop to get the left and top values of a div on > > the screen. We can't be sure of this without a real webpage, URL. > > > > I have been using the code to calculate the top and left values. > > > > var total1 = 0; > > > var total2 = 0; > > > while(element){ > > > total1+=element.offsetTop; > > > total2+=element.offsetLeft; > > > try{ > > > element=element.offsetParent; > > > > }catch(E){ > > > break; > > > } > > > } > > > First, the while statement does not make sense. > > > You want an element (which is going to execute the controlled block) > > that has offsetTop and offsetLeft values to do the controlled block. > > Therefore, your while statement should be > > > while (element.offsetParent) {..controlled block..} > > Just don't pass orphaned elements. I do not understand your "Just don't pass orphaned elements." .. or I'm not sure I understand what you mean to say. If the currently tested element has no offsetParent, then the controlled block is not executed and the execution continues, carries on, goes out of the while loop. Isn't that what is sought here? > > Let's say > > the assignment fails because the current element being examined in > > that while block does not have an offsetParent: why should it > > generates an exception or an error object? > > It definitely can. Yes, you're right. If the assignment fails, then there should be an exception created. I got mixed up with something else. Sometimes the assignment succeeds but the resulting expression is evaluated as false. if(a = b) {... controlled block ...}; if b == 0, then the controlled block may not be executed even though the assignment was successfully executed. So, it shouldn't be what the coder expected. > > var Element = evt.target ; > > var CalculatedTotalOffsetLeft = CalculatedTotalOffsetTop = 0 ; while > > (Element.offsetParent) > > { > > * CalculatedTotalOffsetLeft += Element.offsetLeft ; > > * CalculatedTotalOffsetTop += Element.offsetTop ; > > * Element = Element.offsetParent ; > > > } ; > > > OffsetXForNS6 = evt.pageX - CalculatedTotalOffsetLeft ; > > OffsetYForNS6 = evt.pageY - CalculatedTotalOffsetTop ; > That doesn't take borders into account. *If you _must_ measure all the > way to the document origin (virtually never), use > getBoundingClientRect. Borders. This is news to me. I'll have to verify this some day. > > > For the same DOM TREE this code is giving a performance reading of > > > 30msec in IE8 and 80 to 200msec in IE6. I want to gain a considerable > > > performance improvement in IE6. > > > IE6 <sigh .. *Why do you need to support IE6?>. > > Because lots of corporate users are stuck with it? *And because > there's virtually no difference with IE7. As far as offsetParent, offsetLeft and offsetTop are involved, there is very little difference between IE6 and IE7: only 1 difference, IIRC. There are some/more differences between IE 6 and IE 7 with regards to positioniseverything.net bugs Explorer Exposed http://www.positioniseverything.net/explorer.html >*And then there are the > browsers that copied IE6 oddities... > > > Imagine that people > > are less and less using that browser and that IE8 implemented an > > improved offsetParent, offsetLeft and offsetTop model. > > Yes, less people are using IE6. *IE6-8 (and perhaps earlier) all have > getBoundingClientRect (and other browsers have copied). *But it is > much simpler and less problematic to measure to the origin of a > positioned ancestor. When the ancestor is positioned (non-static), yes. But what happens when the elements are within a table or within nested tables? I think you still have to resort to offsetParent. > > With those numbers, don't you want to tell your IE6 users to upgrade > > or to switch? It would solve many many problems... > > Because you can't tell users to upgrade their browsers. I try to invite them to upgrade and try to address their intelligence at all times. Ultimately, it's all up to them to decide. But I don't hide (or don't try to hide) to them that IE6 is very buggy, unreliable, not-trustworthy, etc. I certainly want them to know and understand that web-capable softwares (or any kind of softwares) should be using the latest stable available version for all kinds of reasons: security, bug fixes, stability, speed, accessibility and usability features, etcetctectcc. >*Some can't > upgrade them. * Often, these people can still/nevertheless install an alternate browser. In June 11th 2009 in the US, television signal stopped being for analog tv; about 15% of users could no longer get local tv. In Canada, the deadline for getting ready for digital-only tv signal is august 2011. It's roughly the same for other countries regarding the transition to digital tv. If you have to buy a new tv because of technological reasons (and FCC and government rules), then I don't understand why corporate users can not upgrade their browser softwares. They certainly were warned in the past that their applications shouldn't be entirely dependent on Microsoft products and Microsoft Windows platform. The top 20 IT mistakes to avoid, November 19, 2004 11. Developing Web apps for IE only http://www.infoworld.com/d/developer...d-314?page=0,3 > Some don't know what a browser _is_. * Those (like seniors) are the most difficult portion of the market. There's very little you can do ... unless you're a friend of them and you visit them at home. [snipped] > > And if you are open to all ideas, then add a IE6nomore or IE6RIP > > button in your webpage so that people get the message that IE6 is > > buggy, not recommendable, etc. should upgrade or switch. > > Hell no. *You _never_ insult the user's browser. *They may not be able > (or know how) to upgrade. If your message is not agressive or on purpose bashing the browser itself, if you invite diplomatically to upgrade or switch and if you address their intelligence, then those who can may do so. Those who can not .. whatever the reasons .. will not. It is objectively still in the user's best interests to always use the most updated stable release (for countless of reasons) of any software (s)he may be using. regards, Gérard |
|
|
|
|
|||
|
|||
| GTalbot |
|
David Mark
Guest
Posts: n/a
|
On Dec 28, 8:53*pm, GTalbot <newsgr...@gtalbot.org> wrote:
> On 28 déc, 19:33, David Mark <dmark.cins...@gmail.com> wrote: > > > > > On Dec 28, 6:44*pm, GTalbot <newsgr...@gtalbot.org> wrote: > > > > On 28 déc, 12:32, moha297 <moha...@gmail.com> wrote: > > > > > I want to get the top and left values for a div on the screen. > > > > As Peter Michaux replied to you, your description of the problem for > > > which you require assistance is not accurate, too general. An URL > > > would have helped. And maybe, just maybe, you may be wrongly using > > > offsetLeft and offsetTop to get the left and top values of a div on > > > the screen. We can't be sure of this without a real webpage, URL. > > > > > I have been using the code to calculate the top and left values. > > > > > var total1 = 0; > > > > var total2 = 0; > > > > while(element){ > > > > total1+=element.offsetTop; > > > > total2+=element.offsetLeft; > > > > try{ > > > > element=element.offsetParent; > > > > > }catch(E){ > > > > break; > > > > } > > > > } > > > > First, the while statement does not make sense. > > > > You want an element (which is going to execute the controlled block) > > > that has offsetTop and offsetLeft values to do the controlled block. > > > Therefore, your while statement should be > > > > while (element.offsetParent) {..controlled block..} > > > Just don't pass orphaned elements. > > I do not understand your "Just don't pass orphaned elements." .. or > I'm not sure I understand what you mean to say. Orphaned (removed from the document) elements can become ActiveX objects behind the scenes. IIRC, orphaning by innerHTML replacement is a sure bet. if (typeof element.offsetParent == 'unknown') { (element.offsetParent); // Boom } So you wouldn't normally pass such an element to such a function (offset position makes no sense for orphans). The try-catch hides such mistakes. > > If the currently tested element has no offsetParent, then the > controlled block is not executed and the execution continues, carries > on, goes out of the while loop. Isn't that what is sought here? See above. Just evaluating it is enough to trigger an exception. > > > > Let's say > > > the assignment fails because the current element being examined in > > > that while block does not have an offsetParent: why should it > > > generates an exception or an error object? > > > It definitely can. > > Yes, you're right. If the assignment fails, then there should be an > exception created. I got mixed up with something else. > > Sometimes the assignment succeeds but the resulting expression is > evaluated as false. > > if(a = b) {... controlled block ...}; > > if b == 0, then the controlled block may not be executed even though > the assignment was successfully executed. So, it shouldn't be what the > coder expected. > > > > var Element = evt.target ; > > > var CalculatedTotalOffsetLeft = CalculatedTotalOffsetTop = 0 ; while > > > (Element.offsetParent) > > > { > > > * CalculatedTotalOffsetLeft += Element.offsetLeft ; > > > * CalculatedTotalOffsetTop += Element.offsetTop ; > > > * Element = Element.offsetParent ; > > > > } ; > > > > OffsetXForNS6 = evt.pageX - CalculatedTotalOffsetLeft ; > > > OffsetYForNS6 = evt.pageY - CalculatedTotalOffsetTop ; > > That doesn't take borders into account. *If you _must_ measure all the > > way to the document origin (virtually never), use > > getBoundingClientRect. > > Borders. This is news to me. I'll have to verify this some day. Just search the archive or the Web. You have to include the borders (clientLeft/Top) in most browsers (it's something that has to be tested). Some older Opera's were broken in this regard. > > > > > For the same DOM TREE this code is giving a performance reading of > > > > 30msec in IE8 and 80 to 200msec in IE6. I want to gain a considerable > > > > performance improvement in IE6. > > > > IE6 <sigh .. *Why do you need to support IE6?>. > > > Because lots of corporate users are stuck with it? *And because > > there's virtually no difference with IE7. > > As far as offsetParent, offsetLeft and offsetTop are involved, there > is very little difference between IE6 and IE7: only 1 difference, > IIRC. > There are some/more differences between IE 6 and IE 7 with regards to > positioniseverything.net bugs There are a few. Most (if not all) are avoidable. > > Explorer Exposedhttp://www.positioniseverything.net/explorer.html > > >*And then there are the > > browsers that copied IE6 oddities... > > > > Imagine that people > > > are less and less using that browser and that IE8 implemented an > > > improved offsetParent, offsetLeft and offsetTop model. > > > Yes, less people are using IE6. *IE6-8 (and perhaps earlier) all have > > getBoundingClientRect (and other browsers have copied). *But it is > > much simpler and less problematic to measure to the origin of a > > positioned ancestor. > > When the ancestor is positioned (non-static), yes. But what happens > when the elements are within a table or within nested tables? I think > you still have to resort to offsetParent. I didn't mean that you didn't have to use offsetParent at all. I mean limit the number of "hops" and stop short of the body (unless it has position:relative). > > > > With those numbers, don't you want to tell your IE6 users to upgrade > > > or to switch? It would solve many many problems... > > > Because you can't tell users to upgrade their browsers. > > I try to invite them to upgrade and try to address their intelligence > at all times. See, the typical user don't want to hear about their intelligence. Thin ice. > Ultimately, it's all up to them to decide. But I don't > hide (or don't try to hide) to them that IE6 is very buggy, > unreliable, not-trustworthy, etc. That's ridiculous. If your page is buggy and/or unreliable, the user will (rightfully) blame _you_. No amount of blaming the browser will move them (as well it should not). > I certainly want them to know and > understand that web-capable softwares (or any kind of softwares) > should be using the latest stable available version for all kinds of > reasons: security, bug fixes, stability, speed, accessibility and > usability features, etcetctectcc. > > >*Some can't > > upgrade them. * > > Often, these people can still/nevertheless install an alternate > browser. Some can and some can't. > > In June 11th 2009 in the US, television signal stopped being for > analog tv; about 15% of users could no longer get local tv. In Canada, > the deadline for getting ready for digital-only tv signal is august > 2011. It's roughly the same for other countries regarding the > transition to digital tv. > If you have to buy a new tv because of technological reasons (and FCC > and government rules), then I don't understand why corporate users can > not upgrade their browser softwares. You don't need to understand everything to be successful. > They certainly were warned in the > past that their applications shouldn't be entirely dependent on > Microsoft products and Microsoft Windows platform. > > The top 20 IT mistakes to avoid, November 19, 2004 > 11. Developing Web apps for IE onlyhttp://www.infoworld.com/d/developer-world/top-20-it-mistakes-avoid-3... That's another topic altogether. Some corporate users are simply stuck with IE6/7 and will be for years. In most cases, it has nothing to do with building IE-only websites. > > > Some don't know what a browser _is_. * > > Those (like seniors) are the most difficult portion of the market. Lots of PC-savvy corporate users are clueless about browsers and/or disallowed from making decisions about which browsers to use. > There's very little you can do ... unless you're a friend of them and > you visit them at home. Sounds like a very hard way to go. They won't let you waltz into corporate sites changing browsers anyway. I don't think you have time to visit every "senior" at home either. So tackle the problem from the other end. > > [snipped] > > > > And if you are open to all ideas, then add a IE6nomore or IE6RIP > > > button in your webpage so that people get the message that IE6 is > > > buggy, not recommendable, etc. should upgrade or switch. > > > Hell no. *You _never_ insult the user's browser. *They may not be able > > (or know how) to upgrade. > > If your message is not agressive or on purpose bashing the browser > itself, if you invite diplomatically to upgrade or switch and if you > address their intelligence, then those who can may do so. Those who > can not .. whatever the reasons .. will not. Nobody wants to hear about it from your site, unless your site is about browsers. > > It is objectively still in the user's best interests to always use the > most updated stable release (for countless of reasons) of any software > (s)he may be using. See above for various reasons why not. |
|
|
|
|
|||
|
|||
| David Mark |
|
Garrett Smith
Guest
Posts: n/a
|
Thomas 'PointedEars' Lahn wrote:
> moha297 wrote: > >> I want to get the top and left values for a div on the screen. >> >> I have been using the code to calculate the top and left values. >> >> var total1 = 0; >> var total2 = 0; >> while(element){ >> total1+=element.offsetTop; >> total2+=element.offsetLeft; >> try{ >> element=element.offsetParent; >> }catch(E){ > > Should be `e' as it does not refer to a constructor. > Right. One more consideration is that IE (JScript, actually) will add the identifier not to catch-block's scope, but to the function's variable object. In an event handler callback, it is common to use - e - for the event parameter. When a function with variable - e- reatches a catch block and the catch block has `e`, then in IE, the function's variable `e` refers to the Error in IE. Example: document.body.onclick = function(e) { e = e || window.event; try { throw new Error("catch scope bugs!");; } catch(e) { //<-- XXX JScript bug, replaced e's value with error. } finally { alert(e.message || "Pass:" + e.type); } }; IE elerts: "catch scope bugs!" The convention I have adopted is to name exception identifier as - ex - and event as - ev -. -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/ |
|
|
|
|
|||
|
|||
| Garrett Smith |
|
|
|
| |
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| RTL and offsetLeft in IE 6 | Andrew Poulos | Javascript | 25 | 05-13-2009 02:08 AM |
| Trouble finding left/top offset position of a DIV when parent DIVhas a border | Stevo | Javascript | 10 | 03-27-2008 04:43 PM |
| offsetLeft/Top Bug in Firefox? (value is 8) | Keith Thornhill | Javascript | 4 | 08-16-2004 09:04 PM |
| Different offsetLeft/Top calculation for elements contained in DIV and TABLE/TD | Pieter Van Waeyenberge | Javascript | 2 | 02-16-2004 02:21 PM |
| offsetLeft & Top differ for different element types? | Pieter Van Waeyenberge | Javascript | 0 | 02-15-2004 04:31 PM |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc..
SEO by vBSEO ©2010, Crawlability, Inc. |




