473,549 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.g etElementsByTag Name) ||
!(basehrefs = document.getEle mentsByTagName( 'base')) ||
!(basehrefs.len gth) || !(basehrefs.len gth > 0) ||
!(basehrefs[0].getAttribute) ||
!(basehref = basehrefs[0].getAttribute(' href')) )
basehref = '';

This is essentially a very cautious version of:

basehref = doument.getElem entsByTagName(' 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 9196
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.g etElementsByTag Name) ||
This is okay
!(basehrefs = document.getEle mentsByTagName( '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.ge tElementsByTagN ame ||
!document.getEl ementsByTagName ('base') ||
!document.getEl ementsByTagName ('base')[0] ||
!document.getEl ementsByTagName ('base')[0].getAttribute(' href')
) {
basehref = '';
....

Although I think that a basic

if ( document.getEle mentsByTagName ) {
basehref = document.getEle mentsByTagName( 'base');
if (basehref.lengt h) {
// Do your stuff
} else {
basehref = "";
}
} else {
basehref = "";
}

will be sufficient...
JW

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


This will always evaluate to false


Not if document.getEle mentsByTagName( '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.ge tElementsByTagN ame ||
!document.getEl ementsByTagName ('base') ||


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

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

!(basehrefs = document.getEle mentsByTagName( 'base')) ||


This will always evaluate to false


Not if document.getEle mentsByTagName( 'base') returns null.


document.getEle mentsByTagName( '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.getEle mentsByTagName( 'base');
alert(basehrefs ? 1 : 0);
</script>
</head>
<body>
</body>
</html>
JW

Jul 20 '05 #4
"Janwillem Borleffs" <jw@jwscripts.c om> writes:
document.getEle mentsByTagName( '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.getEle mentsByTagName( 'base')).length ||

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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.getEle mentsByTagName( '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.c om> writes:
Lasse Reichstein Nielsen wrote:
So the better test would be
!(basehrefs = document.getEle mentsByTagName( '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/rasterTriangleD OM.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
1932
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
2093
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 conversion, according to my compiler. Seems there are too many constructors and operator conversions. But this code works:
3
1613
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. href=index2.php?frame2 <html> <head> <? $tag = $_SERVER; if(empty($tag)) {; } else { echo "<base target=$tag>"; } ?> </head>
1
2714
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 http://services.ccagroup.co.uk/upbin/oformw?&whsmithwedding (no referrer supplied, so I can't be certain), which contains <base...
3
4578
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 more maintainable. Otherwise, we have to manually output the application root eg. src="<%= AppRoot %>". This is cumbersome and hard to maintain. ...
0
1482
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 collection's types are all derived from a base class Base i.e X : Base, Y : Base etc etc.
3
12838
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 to create an index page in every sub-directory. The structure is like the following: / /sources/ /docs/ /docs/doc1 /share
16
2373
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
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
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...
1
7470
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...
0
7809
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...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
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...
0
3500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
763
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...

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.