Getting href attribute of <base> tag?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Weston C

    #1

    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...
  • Janwillem Borleffs

    #2
    Re: Getting href attribute of &lt;base&gt; tag?

    Weston C wrote:[color=blue]
    > 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) ||[/color]

    This is okay
    [color=blue]
    > !(basehrefs = document.getEle mentsByTagName( 'base')) ||[/color]

    This will always evaluate to false
    [color=blue]
    > This is essentially a very cautious version of:[/color]

    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



    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Getting href attribute of &lt;base&gt; tag?

      "Janwillem Borleffs" <jw@jwscripts.c om> writes:
      [color=blue]
      > Weston C wrote:[/color]
      [color=blue][color=green]
      >> !(basehrefs = document.getEle mentsByTagName( 'base')) ||[/color]
      >
      > This will always evaluate to false[/color]

      Not if document.getEle mentsByTagName( 'base') returns null.
      [color=blue]
      > 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') ||[/color]

      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 - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Janwillem Borleffs

        #4
        Re: Getting href attribute of &lt;base&gt; tag?

        Lasse Reichstein Nielsen wrote:[color=blue]
        > "Janwillem Borleffs" <jw@jwscripts.c om> writes:
        >[color=green]
        >> Weston C wrote:[/color]
        >[color=green][color=darkred]
        >>> !(basehrefs = document.getEle mentsByTagName( 'base')) ||[/color]
        >>
        >> This will always evaluate to false[/color]
        >
        > Not if document.getEle mentsByTagName( 'base') returns null.
        >[/color]

        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



        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Getting href attribute of &lt;base&gt; tag?

          "Janwillem Borleffs" <jw@jwscripts.c om> writes:
          [color=blue]
          > document.getEle mentsByTagName( 'base') is never null, it always returns an
          > object.[/color]

          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 - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Janwillem Borleffs

            #6
            Re: Getting href attribute of &lt;base&gt; tag?

            Lasse Reichstein Nielsen wrote:[color=blue]
            > So the better test would be
            > !(basehrefs = document.getEle mentsByTagName( 'base')).length ||
            >[/color]

            No, it would not, because this would only store the value returned by the
            length property in basehrefs.


            JW



            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: Getting href attribute of &lt;base&gt; tag?

              "Janwillem Borleffs" <jw@jwscripts.c om> writes:
              [color=blue]
              > Lasse Reichstein Nielsen wrote:[color=green]
              >> So the better test would be
              >> !(basehrefs = document.getEle mentsByTagName( 'base')).length ||[/color][/color]
              [color=blue]
              > No, it would not, because this would only store the value returned by the
              > length property in basehrefs.[/color]

              Not the way I count. The assignment is inside the outer parentheses,
              the lenght property access is outside.

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • Janwillem Borleffs

                #8
                Re: Getting href attribute of &lt;base&gt; tag?

                Lasse Reichstein Nielsen wrote:[color=blue]
                > Not the way I count. The assignment is inside the outer parentheses,
                > the lenght property access is outside.
                >[/color]

                Point taken


                JW



                Comment

                Working...