Getting href attribute of <base> tag?

W

Weston C

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...
 
J

Janwillem Borleffs

Weston said:
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
 
L

Lasse Reichstein Nielsen

Janwillem Borleffs said:
Weston C wrote:

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
 
J

Janwillem Borleffs

Lasse said:
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
 
L

Lasse Reichstein Nielsen

Janwillem Borleffs said:
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
 
J

Janwillem Borleffs

Lasse said:
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
 
L

Lasse Reichstein Nielsen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top