Get the URL base

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Genisio

    #1

    Get the URL base

    What is the most reliable way to grab the URL base for a document?

    I can get window.location .protocol + window.location .host +
    window.location .path... and that would work most the time, but if the
    <BASE> tag is used, the URL base changes to that tag.

    Is there a one-stop-shop location to retreive the URL base of the document?

    Thanks,
    Brian

  • Michael Winter

    #2
    Re: Get the URL base

    On Wed, 21 Jan 2004 09:50:52 -0500, Brian Genisio <BrianGenisio@y ahoo.com>
    wrote:
    [color=blue]
    > What is the most reliable way to grab the URL base for a document?
    >
    > I can get window.location .protocol + window.location .host +
    > window.location .path... and that would work most the time, but if the
    > <BASE> tag is used, the URL base changes to that tag.
    >
    > Is there a one-stop-shop location to retreive the URL base of the
    > document?[/color]

    Try:

    var baseAddr = null;

    if( document.getEle mentsByTagName ) {
    var elems = document.getEle mentsByTagName( 'base' );

    if( elems.length ) {
    baseAddr = elems[ 0 ].href;
    }
    }

    If 'baseAddr' is null, try parsing the document URL.

    Of course, you might not actually need the base address; it depends what
    you're using it for.

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Brian Genisio

      #3
      Re: Get the URL base

      Michael Winter wrote:[color=blue]
      > On Wed, 21 Jan 2004 09:50:52 -0500, Brian Genisio
      > <BrianGenisio@y ahoo.com> wrote:
      >[color=green]
      >> What is the most reliable way to grab the URL base for a document?
      >>
      >> I can get window.location .protocol + window.location .host +
      >> window.location .path... and that would work most the time, but if the
      >> <BASE> tag is used, the URL base changes to that tag.
      >>
      >> Is there a one-stop-shop location to retreive the URL base of the
      >> document?[/color]
      >
      >
      > Try:
      >
      > var baseAddr = null;
      >
      > if( document.getEle mentsByTagName ) {
      > var elems = document.getEle mentsByTagName( 'base' );
      >
      > if( elems.length ) {
      > baseAddr = elems[ 0 ].href;
      > }
      > }
      >
      > If 'baseAddr' is null, try parsing the document URL.
      >
      > Of course, you might not actually need the base address; it depends what
      > you're using it for.
      >
      > Mike
      >[/color]

      What I need it for is tricky... I am writing my own DOM (not in JS), and
      my application that uses the DOM needs to know the base URL for
      retrieving links with relative paths. When I realized I didnt have it
      anywhere in the DOM model, I thought that possibly I was missing
      something. I wanted to be able to put in the same place the JS engine
      would find it. But, if there is no direct method for retrieving it, I
      have no issue.

      I can create a method that is not exported to the JS engine for the
      application.

      Brian

      Comment

      Working...