473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to detect the absolute position (top, left) of an element (also for IE) ?

I create elements dynamically on a webpage using AJAX. I add events
(like onclick). Everything works fine I can retrace the origin of the
event, the parentnode, ... but now I have to put a layer (a form) on
the image from which the event originated. I have foun some code on the
web that works fine for FF and Opera, but not for IE (6 & 7). The
top-position is good, but not the left (more then 100px to the left of
for IE). I have found another method that gets the absolute x-position
of an element but works only good for IE 6 & 7. How do I distinguish
between browsers ? Normally you test wether an object exists like if
(document.all) ... Problem: Opera also knows how to handle
document.all. For the moment I use this:

function getXYCoordinates(obj) {
var curleft = curtop = 0;
var x = obj.offsetLeft; // for IE
if (obj.offsetParent) {
curleft = obj.offsetLeft;
curtop = obj.offsetTop;
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
}
}
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}

This works for FF and IE 6 & 7 but not for Opera. If I skip the test
(document.all) and return [curleft, curtop] it works fine for FF and
Opera but not for IE 6 & 7.
How can I test an relevant object to determine wether I should return
[x, curtop] instead of [curleft, curtop] ?
Or is there a better method to determine the position of an object that
raised an event.

thanx,

Pugi!

Dec 19 '06 #1
10 1783

Pugi! wrote:
}
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}
if (document.all && user_agent.indexOf("opera") == -1)
{
//IE
}
else
{
//FF,Opera
}

Dec 19 '06 #2
marss said the following on 12/19/2006 5:05 AM:
Pugi! wrote:
> }
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}

if (document.all && user_agent.indexOf("opera") == -1)
{
//IE
No, it is a browser that supports document.all and doesn't have opera in
the userAgent string. It does *NOT* mean it is IE.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 19 '06 #3

var user_agent = navigator.userAgent.toLowerCase();
if (document.all && user_agent.indexOf("opera") == -1)
{
//IE
}
else
{
//FF,Opera
}

Dec 19 '06 #4

Randy Webb wrote:
marss said the following on 12/19/2006 5:05 AM:
Pugi! wrote:
}
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}
if (document.all && user_agent.indexOf("opera") == -1)
{
//IE

No, it is a browser that supports document.all and doesn't have opera in
the userAgent string. It does *NOT* mean it is IE.
This is advice on specific case.
Simplified for clarity.

Dec 19 '06 #5
marss said the following on 12/19/2006 5:15 AM:
Randy Webb wrote:
>marss said the following on 12/19/2006 5:05 AM:
>>Pugi! wrote:

}
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}

if (document.all && user_agent.indexOf("opera") == -1)
{
//IE
No, it is a browser that supports document.all and doesn't have opera in
the userAgent string. It does *NOT* mean it is IE.

This is advice on specific case.
Simplified for clarity.
It is still useless advice that teaches people to think the
navigator.userAgent string can be used to identify a browser when it
can't. Want a test that will, without a doubt, identify Opera?

if (window.opera){alert('YOU ARE USING OPERA!!!')

Anything else with regards to the user agent string identify the browser
is unmitigated garbage.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
Dec 19 '06 #6

Randy Webb wrote:
It is still useless advice
This variant can solve concrete case no matter what do you think about
it. It longer than your solution but it is working.
that teaches people to think the
navigator.userAgent string can be used to identify a browser when it
can't.
If people CAN identify Opera by navigator.userAgent than they MAY think
that navigator.userAgent string can be used to identify Opera.
Want a test that will, without a doubt, identify Opera?

if (window.opera){alert('YOU ARE USING OPERA!!!')

Anything else with regards to the user agent string identify the browser
is unmitigated garbage.
Hard to be a God, eh?

Dec 19 '06 #7
MB
Randy Webb skrev:
marss said the following on 12/19/2006 5:15 AM:
>Randy Webb wrote:
>>marss said the following on 12/19/2006 5:05 AM:
Pugi! wrote:

}
if (document.all) { // here I should test for IE, but also
Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}
>
if (document.all && user_agent.indexOf("opera") == -1)
{
//IE
No, it is a browser that supports document.all and doesn't have opera in
the userAgent string. It does *NOT* mean it is IE.

This is advice on specific case. Simplified for clarity.

It is still useless advice that teaches people to think the
navigator.userAgent string can be used to identify a browser when it
can't. Want a test that will, without a doubt, identify Opera?

if (window.opera){alert('YOU ARE USING OPERA!!!')

Anything else with regards to the user agent string identify the browser
is unmitigated garbage.
Hi Randy.
Sorry, but your code will not work and is just garbage.

Quote from http://www.javascriptmall.com/learn/lesson5.htm:

"You use the if statement in JavaScript to make decisions. The syntax
for it is as follows:

if (condition){
statements
}

The if keyword identifies this as an if statement. The condition in the
parenthesis ( ) is evaluated to determine if true, and if so then the
statements inside the curly braces { } are executed, otherwise they are
skipped and the programs continues with the first line after the if
statement."

In other words, you can write one of the following:

simplest:

if (window.opera) alert('YOU ARE USING OPERA!!!');

....or a more correct and even working use of curly braces:

if (window.opera){alert('YOU ARE USING OPERA!!!');}

....or the above in a bit more easy to read way:

if (window.opera) {
alert('YOU ARE USING OPERA!!!');
}

I hope this helps you a bit. Javascript is not always easy to learn for
the beginner but I'm sure you'll soon get the hang of it.
Have a nice day. Be nice to people and they will be nice back making
your entire day nice. :)

/MB
Dec 19 '06 #8
MB wrote:
<snip>
Quote from http://www.javascriptmall.com/learn/lesson5.htm:

"You use the if statement in JavaScript to make decisions. The syntax
for it is as follows:

if (condition){
statements
}

The if keyword identifies this as an if statement. The condition in the
parenthesis ( ) is evaluated to determine if true, and if so then the
statements inside the curly braces { } are executed, ...
<snip ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The curly braces are a statement in javascript; a Block statement. The
- if - statement only conditionally executes a single statement, which
may be a Block statement and so its execution may imply the execution
of numerous contained statements.

Richard.

Dec 19 '06 #9
Pugi! wrote:
Or is there a better method to determine the position of an object
that raised an event.
http://www.JavascriptToolbox.com/lib/objectposition/

That's the most robust generalized solution I know of.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 19 '06 #10
marss said the following on 12/19/2006 7:31 AM:
Randy Webb wrote:
>It is still useless advice

This variant can solve concrete case no matter what do you think about
it. It longer than your solution but it is working.
It isn't so much a question of whether it works - in this scenario - as
it a question of whether it is a good practice or not to depend on the
userAgent string when that string can be altered.
>that teaches people to think the
navigator.userAgent string can be used to identify a browser when it
can't.

If people CAN identify Opera by navigator.userAgent than they MAY think
that navigator.userAgent string can be used to identify Opera.
It can also lead to people thinking they can identify Mozilla, Internet
Explorer, iCab and other browsers by the navigator.userAgent string
which it can't. A test of the navigator.userAgent string in my MSIE7 for
the letters "MSIE" returns false.
>Want a test that will, without a doubt, identify Opera?

if (window.opera){alert('YOU ARE USING OPERA!!!')

Anything else with regards to the user agent string identify the browser
is unmitigated garbage.

Hard to be a God, eh?
Nothing to do with being a "God", it has to do with trying to write as
bullet-proof a solution as possible and that solution - even in this
case - doesn't include the userAgent string.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 19 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ken Kast | last post by:
Here's my situation. I have a statically positioned table that has an image in a cell. I also have some layers, defined by absolute-positioned DIVs for some animation. Everything works until I...
2
by: Csaba2000 | last post by:
Demo page at http://csaba.org/Demos/getRect.htm One question I've seen come up multiple times is: How can I find the abolute position of my favorite element on the page? The standard answer is...
4
by: Peter Pfannenschmid | last post by:
Hello experts, I would be very grateful if you would have a look at the following URL: http://www.omeganet.de/test.html The html file and the associated style have been validated...
4
by: mike eli | last post by:
Hi, I have several absolute positioned elements inside an absolute positioned DIV. I would like one of the nested elements to have a dynamic width. I set it's left and right attributes to 5, so...
8
by: TJ Walls | last post by:
Hello All, I am fairly new to html authoring and have run into a strange problem. I have a simple GIF file that is a black horizontal line. As a test start I am trying to display it twice with...
7
by: Alex | last post by:
Hi Everone, I need some advice on how to setup 4 columns where the outside two are absolute (120px) and the inner two (side by side) are relevent (Fluid) and change with the screen. Here's my...
3
by: horusprim | last post by:
Is there a CSS absolute positioning rendering bug in FF1.02 and IE 6? I have been experimenting with precision absolute positioning in CSS. The following test content was used in the...
2
by: nonsensitor | last post by:
I'm just learning xhtml & css, primarily from westciv's online tutorials and css guide. I've run into a couple of problems with a page I'm developing that I can't figure out. The first problem is...
7
by: chemlight | last post by:
I am having an issue with HTML elements not printing when positioned absolutely when they extend beyond the first page. I am working on some foreign tax refund forms. The forms are cut into multiple...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.