Getting href attribute of <base> tag?

Discussion in 'Javascript' started by Weston C, Jan 2, 2004.

  1. Weston C

    Weston C Guest

    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...
    Weston C, Jan 2, 2004
    #1
    1. Advertising

  2. 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
    Janwillem Borleffs, Jan 3, 2004
    #2
    1. Advertising

  3. "Janwillem Borleffs" <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jan 3, 2004
    #3
  4. Lasse Reichstein Nielsen wrote:
    > "Janwillem Borleffs" <> 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
    Janwillem Borleffs, Jan 3, 2004
    #4
  5. "Janwillem Borleffs" <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jan 3, 2004
    #5
  6. 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
    Janwillem Borleffs, Jan 4, 2004
    #6
  7. "Janwillem Borleffs" <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jan 4, 2004
    #7
  8. 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
    Janwillem Borleffs, Jan 4, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    743
    John C. Bollinger
    Jan 27, 2006
  2. Sam

    autopostback and <base href...> tag

    Sam, Jun 29, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    134
  3. John Smith

    BASE HREF tag

    John Smith, Jun 27, 2003, in forum: Javascript
    Replies:
    3
    Views:
    137
    Lasse Reichstein Nielsen
    Jun 27, 2003
  4. just1coder

    Programmatically change base href tag

    just1coder, Apr 29, 2004, in forum: Javascript
    Replies:
    1
    Views:
    159
    Thomas 'PointedEars' Lahn
    May 15, 2004
  5. Vincent van Beveren

    BASE HREF and A HREF="#" onclick="..."

    Vincent van Beveren, Jul 6, 2006, in forum: Javascript
    Replies:
    2
    Views:
    290
    Vincent van Beveren
    Jul 6, 2006
Loading...

Share This Page