473,396 Members | 2,034 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.

getElementById - How to ignore meta tags ?

MNF
Hi everyone,
I am using document.getElementById function in JavaScript to find a
control within an html body, but instead I get back META item, because
incidently the name of one meta tags is the same as the name of my
control. It seems that it is by design, because getElementById returns
the FIRST object with the same ID.

What is the best general workaround to search control only within
body?
I prefer to allow control IDs be the same as meta tag names.

Thanks,
Michael Freidgeim

Please find the sample page to illustrate the issue.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta content="Functional Solutions"
name="keywords">
</HEAD>
<body >
<form name="Form1" method="post" action="" id="Form1">
<input type="text" id="Keywords" >
<script type='text/javascript'>
<!--
SetFocus('Keywords');
function SetFocus(strID, bFindCtrl)
{
ctl = document.getElementById(strID);
// If not found, exit
if(ctl == null || typeof(ctl) == "undefined")
return false;
ctl.focus();//causes error
}
//-->
</script>
</form>
</body>
</HTML>
Jul 23 '05 #1
6 2099
Ivo
"MNF" wrote
Hi everyone,
I am using document.getElementById function in JavaScript to find a
control within an html body, but instead I get back META item, because
incidently the name of one meta tags is the same as the name of my
control. It seems that it is by design, because getElementById returns
the FIRST object with the same ID.

What is the best general workaround to search control only within
body?

I prefer to allow control IDs be the same as meta tag names.


I prefer to see two and two equal five, but have come to accept that it
doesn't work like that. The easiest and simplest as well as the only legal
and valid solution is to make your ID's unique.
HTH
Ivo.
Jul 23 '05 #2
Ivo,

Thank you for your reply.
But META tag has name attribute, not ID and even formally should not be
considered by getElementById. META names and body control IDs are quite
different entities and should not be used in the same namespace.

However let's re-phrase my question: which function should I use/create
for the equivalent of getElementById on the
document.body level?

Michael Freidgeim
Add ".com.au" to my e-mail address to reach me by e-mail

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
On 09 Aug 2004 08:34:30 GMT, Michael Freidgeim <michaelf@librarysolutions>
wrote:
But META tag has name attribute, not ID and even formally shouldnot be
considered by getElementById. META names and body controlIDs are quite
different entities and should not be used in the samenamespace.
We all know that, but Microsoft, in its infinite wisdom, thinks otherwise.
However let's re-phrase my question: which function should Iuse/create
for the equivalent of getElementById on the
document.body level?


There is no pre-existing equivalent, and I don't think that writing one
would be wise; it'll be large and slow. Instead, simply change the id
attribute to something unique, like "key-words".

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #4
Michael Freidgeim wrote:
Ivo,

Thank you for your reply.
But META tag has name attribute, not ID and even formally should not be
considered by getElementById. META names and body control IDs are quite
different entities and should not be used in the same namespace.

However let's re-phrase my question: which function should I use/create
for the equivalent of getElementById on the
document.body level?


The problem is that Internet Explorer will use NAME attributes for
getElementById() lookups, which you've discovered. Ultimately the solution
would be to try to avoid conflicts and ensure that all NAMEs and IDs on the
page are unique. If this is not an option, then the following code will
produce the correctly IDed <DIV> on the <BODY> (even if there are multiple
items with the same name that are not DIVs):

<html>
<head>
<title>Test</title>
<meta name="keywords" content="This is a test META" />
</head>
<body onload="test('keywords');">
<div id="keywords">This is a test DIV</div>
<script type="text/javascript">
function test(theId) {
// retrieve the element
var el = document.getElementById(theId);
// test to ensure it's the kind of tag you want
if (el.tagName.toUpperCase() != 'DIV') {
// if it is not the kind of tag you want, retrieve
// all the tags of the type you want
var divs = document.getElementsByTagName('DIV');
// loop through all the tags of the type you
// want until you find the id you want
for (var i = 0; i < divs.length; i++) {
if (divs[i].id == theId) {
// you found the id you want, assign
// it to "el" and stop looking
el = divs[i];
break;
}
}
}

if (el) {
// if you've successfully retrieve "el", use it
alert(el.firstChild.nodeValue);
}
}
</script>
</body>
</html>

The code could be made less complex by always retrieving
getElementsByTagName(), but I think the getElementById().tagName test is
worth the trouble, considering many browsers will correctly identify the
tagName you want and bypass the loop. Also, if you don't want to identify a
DIV, it should be easy enough to change the code to retrieve the tagName
you do want (or better yet, make it a parameter you can pass to the
function), like:

function getElementByIdOrNameAndTagName(idOrName, tagName) { ...

and call it with:

var el = getElementByIdOrNameAndTagName('keywords', 'DIV');

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #5
Michael Winter wrote:
On 09 Aug 2004 08:34:30 GMT, Michael Freidgeim <michaelf@librarysolutions>
wrote:
But META tag has name attribute, not ID and even formally shouldnot be
considered by getElementById. META names and body controlIDs are quite
different entities and should not be used in the samenamespace.


We all know that, but Microsoft, in its infinite wisdom, thinks otherwise.
However let's re-phrase my question: which function should Iuse/create
for the equivalent of getElementById on the
document.body level?


There is no pre-existing equivalent, and I don't think that writing one
would be wise; it'll be large and slow. Instead, simply change the id
attribute to something unique, like "key-words".

Mike


Not that large, but possibly slow depending how many elements of the tagName
you want are on the current document:

function getElementByIdOrNameAndTagName(idOrName, tagName) {
var el = document.getElementById(idOrName);
if (el.tagName.toUpperCase() != tagName.toUpperCase()) {
var els = document.getElementsByTagName(tagName);
for (var i = 0; i < els.length; i++) {
if (els[i].id == theId) {
el = els[i];
break;
}
}
}
return el;
}

Called using:

var el = getElementByIdOrNameAndTagName('keywords', 'div');

Even in the unlikely event you don't know what tagName you want, you can still
call it with:

var el = getElementByIdOrNameAndTagName('keywords', '*');

although that removes the advantage browsers that correctly identify the right
element to begin with have. If you end up calling the function with '*' a lot,
it might better to remove the getElementById().tagName test entirely.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #6
Thank you guys for your quick responses.
Because my purpose was just to exclude mega tags, I've choosen the
solution based on Grant's getElementByIdOrNameAndTagName.

function getNonMetaElementById(theId) {
var el = document.getElementById(theId);
if (el.tagName.toUpperCase() == "META")
{
var els = document.getElementsByTagName("*");
for (var i = 0; i < els.length; i++) {
if (els[i].id == theId) {
el = els[i];
break;
}
}
} return el;
}

Michael Freidgeim
Add ".com.au" to my e-mail address to reach me by e-mail

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #7

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

Similar topics

1
by: Cezary | last post by:
Hello. I was read PHP manual, but i'm not sure yet. Here is my meta tags in html: <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-2"> <META HTTP-EQUIV="Expires"...
4
by: Brian | last post by:
Hi, I'm trying to use standard meta tags in an xsl doc and using cocoon as my processor. The problem is that cocoon changes for example: <meta name="keywords" content="test, test, test" /> ...
1
by: Darren Blackley | last post by:
Hi there I have documents that I want to automatically add additional meta tags to. The documents already have some meta tags and I want to keep them all together, so I want to add my new meta tags...
19
by: Christian Hvid | last post by:
Hello groups. I have a series of applet computer games on my homepage: http://vredungmand.dk/games/erik-spillet/index.html http://vredungmand.dk/games/nohats/index.html...
24
by: Day Bird Loft | last post by:
Web Authoring | Meta-Tags The first thing to understand in regard to Meta Tags is the three most important tags placed in the head of your html documents. They are the title, description, and...
23
by: Fast Eddie | last post by:
What's the benefit to coding <meta name="author"...> and such? Thanks.
4
by: clintonG | last post by:
Anybody know how to dynamically write the meta tags using code so they are formatted on a separate line in the HTML source? Preferred or optimal framework classes that may be used in this regard? ...
16
by: Edward | last post by:
Hi All, I am having huge problems with a very simple dotnet framework web page (www.gbab.net/ztest3.aspx) , it does NOT render correctly under Apple's Safari. The DIV's do not align amd float as...
4
by: King Coffee | last post by:
Hi, If I use master pages with META tags in the head section... can I still use META tag on the child pages (child pages reference the master page). I ask this question because I heard the more...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.