473,246 Members | 3,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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 9147
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CD | last post by:
Is this possible: class base; class derived; //:public base vector <base*> bList; vector<derived*> dList; //add some derived class pointer entries to dList;
2
by: Siemel Naran | last post by:
This code fails compile std::auto_ptr<Base> f() { std::auto_ptr<Derived> out(new Derived()); return out; } There is ambiguity between a templated constructor and templated operator...
3
by: Gilbert Saint-Flour | last post by:
Hello: I have this one line of PHP code which I'd like to convert to Javascript. The PHP code conditionally issues a BASE statement when the html page is called with a frame name, e.g....
1
by: Chris Sharman | last post by:
I'm seeing quite a few 404 errors, all from an agent/browser which calls itself "Mozilla/4.0 (compatible; BorderManager 3.0)". They appear to be from a built page like...
3
by: SJ | last post by:
Hi, I have a problem with the HTML <base> element, URL re-writing and Postback. We are using URL re-writing on the server, and I'd like to use the base element to make the URLs in the ASPX pages...
0
by: emma_middlebrook | last post by:
Hi Hopefully the title is quite accurate but here's some more information. I have a load of ICollection<references hanging off a class e.g. ICollection<X>, ICollection<Yetc etc. Each of the...
3
by: yan | last post by:
Hello everybody, I am new so this is the occasion to say hello to everybody. I have a problem with absolute/relative paths. I have to create a static documentation in html for a project and I have...
16
by: call_me_anything | last post by:
why is the following not allowed : vector <Base *vec_of_base; vector <Derived *vec_of_derived; vec_of_base = vec_of_derived; Note : The following is allowed :
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.