homepage Welcome to WebmasterWorld Guest from 23.22.179.210
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript Get Website URL
wfernley




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

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 :)

 

Demaestro




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

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]

wfernley




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

Thanks! Not sure if it is working though. It just forwards my browser to [/...]

coopster




msg:3864455
 5:55 pm on Mar 6, 2009 (gmt 0)

hostname
https://developer.mozilla.org/en/DOM/window.location#Properties

wfernley




msg:3865809
 6:57 pm on Mar 8, 2009 (gmt 0)

Thanks Coopster! That does the trick :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved