470,580 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,580 developers. It's quick & easy.

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

Jul 20 '05 #1
2 23825
On Wed, 21 Jan 2004 09:50:52 -0500, Brian Genisio <Br**********@yahoo.com>
wrote:
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?


Try:

var baseAddr = null;

if( document.getElementsByTagName ) {
var elems = document.getElementsByTagName( '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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
Michael Winter wrote:
On Wed, 21 Jan 2004 09:50:52 -0500, Brian Genisio
<Br**********@yahoo.com> wrote:
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?

Try:

var baseAddr = null;

if( document.getElementsByTagName ) {
var elems = document.getElementsByTagName( '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


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

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

52 posts views Thread by Dick Moores | last post: by
2 posts views Thread by Stephan Br?nnimann | last post: by
6 posts views Thread by Paul | last post: by
7 posts views Thread by Jef Driesen | last post: by
5 posts views Thread by Andy | last post: by
1 post views Thread by miben | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.