By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Getting href attribute of <base> tag?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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.