473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getElementById - How to ignore meta tags ?

MNF
Hi everyone,
I am using document.getEle mentById 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="Functi onal Solutions"
name="keywords" >
</HEAD>
<body >
<form name="Form1" method="post" action="" id="Form1">
<input type="text" id="Keywords" >
<script type='text/javascript'>
<!--
SetFocus('Keywo rds');
function SetFocus(strID, bFindCtrl)
{
ctl = document.getEle mentById(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 2142
Ivo
"MNF" wrote
Hi everyone,
I am using document.getEle mentById 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@libra rysolutions>
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('k eywords');">
<div id="keywords">T his is a test DIV</div>
<script type="text/javascript">
function test(theId) {
// retrieve the element
var el = document.getEle mentById(theId) ;
// test to ensure it's the kind of tag you want
if (el.tagName.toU pperCase() != 'DIV') {
// if it is not the kind of tag you want, retrieve
// all the tags of the type you want
var divs = document.getEle mentsByTagName( '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.firstC hild.nodeValue) ;
}
}
</script>
</body>
</html>

The code could be made less complex by always retrieving
getElementsByTa gName(), 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 getElementByIdO rNameAndTagName (idOrName, tagName) { ...

and call it with:

var el = getElementByIdO rNameAndTagName ('keywords', 'DIV');

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #5
Michael Winter wrote:
On 09 Aug 2004 08:34:30 GMT, Michael Freidgeim <michaelf@libra rysolutions>
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 getElementByIdO rNameAndTagName (idOrName, tagName) {
var el = document.getEle mentById(idOrNa me);
if (el.tagName.toU pperCase() != tagName.toUpper Case()) {
var els = document.getEle mentsByTagName( tagName);
for (var i = 0; i < els.length; i++) {
if (els[i].id == theId) {
el = els[i];
break;
}
}
}
return el;
}

Called using:

var el = getElementByIdO rNameAndTagName ('keywords', 'div');

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

var el = getElementByIdO rNameAndTagName ('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*****@agrico reunited.com>
comp.lang.javas cript 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 getElementByIdO rNameAndTagName .

function getNonMetaEleme ntById(theId) {
var el = document.getEle mentById(theId) ;
if (el.tagName.toU pperCase() == "META")
{
var els = document.getEle mentsByTagName( "*");
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
4015
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" CONTENT="0"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0"> <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
4
3725
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" /> to <meta content="test, test, test" name="keywords"> I hope that makes sense. The problem is that I am running some adds on my
1
2614
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 to the end of the existing ones... can someone help me out with a script to do this... example below. <head> <title>The Document Title</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="discription"...
19
3857
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 http://vredungmand.dk/games/platfoot/index.html http://vredungmand.dk/games/minorbug/index.html http://vredungmand.dk/games/timbuktu/index.html http://vredungmand.dk/games/taleban/index.html
24
3553
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 keyword meta-tags. If you are missing any of these meta-tags you are missing the boat. If you use the following meta-tag formula, and you are not trying to deceive the spiders, I guarantee you will succeed in increasing your placement in the...
23
2164
by: Fast Eddie | last post by:
What's the benefit to coding <meta name="author"...> and such? Thanks.
4
2303
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? <meta... /> <meta... /> <meta... /> <%= Clinton Gallagher
16
2518
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 they should, and do in Dotnet. The page is really, really simple, and it has a CSS and with NO masterpage. I have tried using the following as recommendations made earlier in this Newsgroup:-
4
1866
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 pages with META tags containing search engine key words, the higher the search engine ranking. Thanks, King
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.