Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2001
    Posts
    193

    How to get domain name and assign it to a variable with Javascript?

    Can somebody tell me how to go about getting the domain the visitor is accessing my site from (so it will do either domain.com or www.domain.com) and assign it to a variable?

    Here's what I need to do:

    Code:
    var HOST = 'NEED DOMAIN HERE';
    
    setCookie('mtcmtauth', f.author.value, now, '/', HOST, '');
    setCookie('mtcmtmail', f.email.value, now, '/', HOST, '');
    setCookie('mtcmthome', f.url.value, now, '/', HOST, '');

  2. #2
    Join Date
    Jun 2003
    Location
    Janesville, Wi
    Posts
    1,520
    I have contacted one of my Javascript experts to assist.

    Code:
     search_this = ".com";
     full_url = document.URL;
     index = full_url.search(search_this);
     domain = full_url.substring(index + search_this.length);
     
     which can be reduced to:
     
     the_url = document.URL.search(".com");
     HOST = the_url.substring(the_url + 4);
    That will pull back the domain you're on. Where it says .com, change that to match your TLD. You can remove "var HOST" from your script as this sets HOST.

    --Credit to Wray for this response--
    Jakiao

  3. #3
    Join Date
    Oct 2001
    Posts
    193
    Thanks for the help Guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •