472,796 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 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 2065
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.