473,320 Members | 2,048 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,320 software developers and data experts.

What is the preferred way to get a reference to an element in a document?

Hi all,

What is the preferred in term of browser copatability to get a
reference to an element in the document in JavaScript?

I am not sure about when to use document.all or document.layers or
document.getElementByID. Are they all doing the same thing?

Cheers,

dave
Jul 20 '05 #1
3 2381
Rosinger wrote:
What is the preferred in term of browser copatability to get a
reference to an element in the document in JavaScript? I am not sure about when to use document.all or document.layers or
document.getElementByID. Are they all doing the same thing?


Similar things.

The _standard_ way is document.getElementById, but not all browsers support
that. You could use object detection though, possibly something like:

var x;
if (document.getElementById) {
x = document.getElementById('foo');
} else if (document.all) {
...
} else if (document.layers) {
...
}

(Provisio - I'm not quite awake yet, I think the structure is right, but its
quite likely there are syntax errors in there.)

--
David Dorward http://david.us-lot.org/
Which is stupider: Pop-up ads claiming they'll stop spam, or spam claiming
it'll stop pop-up ads? -- Dork Tower
Jul 20 '05 #2
jr*******@hotmail.com (Rosinger) writes:
I am not sure about when to use document.all or document.layers or
document.getElementByID. Are they all doing the same thing?


The preferred way is "document.getElementById". It is from the W3C DOM
specification.

In IE 4 and earlier, that function is not available, so you have to use
the Microsoft proprietary collection "document.all". Apart from being
a collection (so you access it as "document.all[id]" instead of calling
it as a function), it does the same thing as "document.getElementById".

In Netscape 4 and earlier, neither of these two are available. In fact,
there is no way to access an arbitrary element of a page. You can only
access elements through the available collections. Those are
document.images (<img> elements)
document.links (<a href...> and <area href...> elements)
document.ancors (<a name...> elements)
document.forms (<form> elements)
document.applets (<applet> applets)
document.embeds (<embed> elements)
document.layers (<layer> elements and elements with CSS position:absolute)
and
document.forms[...].elements (form elements in a form)

Most of these Netscape collections are also valuid W3C DOM, however
"document.layers" is not. If you are dealing with images, links, or
forms and their elements, you might as well use the collection.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
"Rosinger" <jr*******@hotmail.com> wrote in message
news:d7**************************@posting.google.c om...
| Hi all,
|
| What is the preferred in term of browser copatability to get a
| reference to an element in the document in JavaScript?
|
| I am not sure about when to use document.all or document.layers or
| document.getElementByID. Are they all doing the same thing?
|
| Cheers,
|
| dave

The correct way is using document.getElementById
The browsers that do not support it are better off without javascript
anyway.

Vladdy
Jul 20 '05 #4

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

Similar topics

3
by: Pablo Gomes Ludermir | last post by:
Hello, I have the following case that I am trying to put in XML Schema. I have the classes HelpItem, Document and Message that work as follows: One HelpItem contains several Document and...
1
by: Colin McNaught | last post by:
I'm sure this counts as a newbie question: Take the html expression <a name="thisname" href="newpage.html">GoToIt</a> Inside the <a> tag, 'this' can be used in javascript to refer to the url the...
4
by: comcast news | last post by:
at least i think it this is the problem am trying to fix a webpage/javascript that i didn't write www.deltaneutral.com when invoked or any new page from the menu, get the error msg line 2...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
3
by: Brett | last post by:
What is the following code doing? I see evt and event but what is the difference? <input type="radio" id="us_countryFlag1" name="us_country" onclick="togglePurDec(event)">Yes <script>...
4
by: Brian | last post by:
I am trying to use a web service from a non Microsoft product. When I do a "Add Web Reference" from the Visual Studio dialog box, a request is generated and received by the web services supplier....
6
by: seeIT | last post by:
In a client application a simple webservice (add/multiply) was added to solution panel but proxy methods do not appear. WSDL description in solution panel follows: <?xml version="1.0"...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
5
by: dwmartin18 | last post by:
Hello everyone. I have quite the puzzling problem with a script I have been working on lately. I have created a function that can be called to create a new html element (e.g. input, select, div,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.