Welcome to WebmasterWorld Guest from 184.73.8.127

Forum Moderators: open

Message Too Old, No Replies

Javascript Get Website URL

   
5:22 pm on Mar 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi everyone,

How do I get Javascript to tell me the website url.

For example if I have a page www.example.com/page.html

I want Javascript to tell me the site url is www.example.com and not www.example.com/page.html (which document.location tells me)

Is there a way to do this? If so, how?

Thanks in advance for your help :)

5:33 pm on Mar 6, 2009 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If document.location doesn't return http:// then you can just split the string on '/' and use the first part.

var myLoc = document.location;
var myURLParts = myLoc.split("/");
var myURL = myURLParts[0];

If it does return http:// replace it with "" before, like this.

var myLoc = document.location;
myLoc = myLoc.replace("http://", "");
var myURLParts = myLoc.split("/");
var myURL = myURLParts[0];

[edited by: Demaestro at 5:34 pm (utc) on Mar. 6, 2009]

5:48 pm on Mar 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks! Not sure if it is working though. It just forwards my browser to [/...]
5:55 pm on Mar 6, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



hostname
[developer.mozilla.org...]
6:57 pm on Mar 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Coopster! That does the trick :)