470,591 Members | 1,458 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

Getting href attribute of <base> tag?

I'm trying to get the href attribute from the base tag in the document
head. The following does not seem to work... any ideas what I'm doing
wrong?
if(!(document.getElementsByTagName) ||
!(basehrefs = document.getElementsByTagName('base')) ||
!(basehrefs.length) || !(basehrefs.length > 0) ||
!(basehrefs[0].getAttribute) ||
!(basehref = basehrefs[0].getAttribute('href')) )
basehref = '';

This is essentially a very cautious version of:

basehref = doument.getElementsByTagName('base')[0].getAttribute('href')

The big hairy if just provides checks to make sure everything can be
done and provide a contingency if it doesn't...
Jul 20 '05 #1
7 8640
Weston C wrote:
I'm trying to get the href attribute from the base tag in the document
head. The following does not seem to work... any ideas what I'm doing
wrong?
if(!(document.getElementsByTagName) ||
This is okay
!(basehrefs = document.getElementsByTagName('base')) ||
This will always evaluate to false
This is essentially a very cautious version of:


Its sure as and I'm wondering if it makes sence. However, if you want to
take this approach, the following should do it:

if(!document.getElementsByTagName ||
!document.getElementsByTagName('base') ||
!document.getElementsByTagName('base')[0] ||
!document.getElementsByTagName('base')[0].getAttribute('href')
) {
basehref = '';
....

Although I think that a basic

if ( document.getElementsByTagName ) {
basehref = document.getElementsByTagName('base');
if (basehref.length) {
// Do your stuff
} else {
basehref = "";
}
} else {
basehref = "";
}

will be sufficient...
JW

Jul 20 '05 #2
"Janwillem Borleffs" <jw@jwscripts.com> writes:
Weston C wrote:
!(basehrefs = document.getElementsByTagName('base')) ||


This will always evaluate to false


Not if document.getElementsByTagName('base') returns null.
Its sure as and I'm wondering if it makes sence. However, if you want to
take this approach, the following should do it:

if(!document.getElementsByTagName ||
!document.getElementsByTagName('base') ||


Giving the return value of document.getElementsByTagName('base') a
name here, saves the two next calls. That is the only difference
betweem
!document.getElementsByTagName('base')
and
!(basehrefs = document.getElementsByTagName('base'))

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Lasse Reichstein Nielsen wrote:
"Janwillem Borleffs" <jw@jwscripts.com> writes:
Weston C wrote:

!(basehrefs = document.getElementsByTagName('base')) ||


This will always evaluate to false


Not if document.getElementsByTagName('base') returns null.


document.getElementsByTagName('base') is never null, it always returns an
object.

Try the following which alerts 1:

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
basehrefs = document.getElementsByTagName('base');
alert(basehrefs ? 1 : 0);
</script>
</head>
<body>
</body>
</html>
JW

Jul 20 '05 #4
"Janwillem Borleffs" <jw@jwscripts.com> writes:
document.getElementsByTagName('base') is never null, it always returns an
object.


Good point. Must not confuze it with getElementById!
It always return a NodeList. Some times it is just empty.
So the better test would be
!(basehrefs = document.getElementsByTagName('base')).length ||

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Lasse Reichstein Nielsen wrote:
So the better test would be
!(basehrefs = document.getElementsByTagName('base')).length ||


No, it would not, because this would only store the value returned by the
length property in basehrefs.
JW

Jul 20 '05 #6
"Janwillem Borleffs" <jw@jwscripts.com> writes:
Lasse Reichstein Nielsen wrote:
So the better test would be
!(basehrefs = document.getElementsByTagName('base')).length ||
No, it would not, because this would only store the value returned by the
length property in basehrefs.


Not the way I count. The assignment is inside the outer parentheses,
the lenght property access is outside.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
Lasse Reichstein Nielsen wrote:
Not the way I count. The assignment is inside the outer parentheses,
the lenght property access is outside.


Point taken
JW

Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Siemel Naran | last post: by
3 posts views Thread by Gilbert Saint-Flour | last post: by
1 post views Thread by Chris Sharman | last post: by
3 posts views Thread by SJ | last post: by
16 posts views Thread by call_me_anything | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.