SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,482
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    seperating the domain & tld from the URL?

    hi
    I am trying to make a bookmarklet to get the whois info of the domain that the user is currently browsing. I'll be using the services of http://www.webhosting.info as any domain & tld passed to its whois server at http://whois.webhosting.info/ gives the whois info of that domain. So to know Google.com's whois info, we go to the URL http://whois.webhosting.info/google.com

    Now as I get it, the URL of the current domain can be had through
    HTML Code:
    var cURL = window.parent.location.href;
    But what if the URL is like http://www.example.com/mypage.asp? The above will return it all but only example.com is required from the URL as http://whois.webhosting.info won't accept the whole url, it'll not even accept www.example.com.
    So, can anyone tell me how do I extract the domain & tld from the URL? It needs to be a small piece of code, as it is to be put in a bookmarklet which can have only 255 characters.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  2. #2
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about you pass the whole thing into split(".")?
    You'd check to see if array[1] was one of the tld's (com, net, org etc).
    If it's not, then it should be in array[2].

    Ex: passed domain is example.com
    Code:
    array[0] = domain
    array[1] = com
    myDomain = array[0] + "." + array[1];
    or

    passed domain is htttp://www.site.com/stuff.php
    Code:
    array[0] = http://www
    array[1] = domain
    array[2] = com/stuff
    array[3] = php
    array2 = array[2].split("/")
    myDomain = array[1] + "." + array2[0];
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  3. #3
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,482
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That'll be too much if/else usage. I was thinking of some kind of RegExp way of doing it. suppose if the URL is like http://www.bb.example.com/pages/my.php then? There's an added sub-domain in here now, & we don't want that sub-domain.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, regex exeeds myknowledge, so I'm afraid I can't help ya anymore...
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,482
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    no problem. maybe someone else can.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  6. #6
    SitePoint Addict
    Join Date
    Jul 2000
    Posts
    201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about this (extended from Defender's idea) ...
    Code:
    function getsld(){
    var cURL="http://www.bb.example.com/pages/my.php";
    var array=cURL.split("/")[2].split(".");
    var sld=array[array.length-2]+"."+array[array.length-1]
    alert(sld);
    }
    edit: shortened it by 1 line

  7. #7
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,482
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Great!! This works.
    Thanks for the function. Now my bookmarklet is
    complete & it works. I've made 2 versions of it, one
    opens the WHOIS info of the domain in the same
    window & other opens it in a new window.

    Here are the codes for them.

    Same window WHOIS info
    Code:
    function getsld(){var url = window.parent.location.href;var array1=url.split("/");var array2=array1[2].split(".");var sld=array2[array2.length-2]+"."+array2[array2.length-1];window.parent.location.href="http://whois.webhosting.info/"+sld;}getsld();
    New Window WHOIS info
    Code:
    function getsld(){var url=window.parent.location.href;var array1=url.split("/");var array2=array1[2].split(".");var sld=array2[array2.length-2]+"."+array2[array2.length-1];var whoisU="http://whois.webhosting.info/"+sld;window.open(whoisU, "whois");}getsld();
    I've attached both bookmarklets in a ZIP file. They are ready to use.
    You just have to place them in your Favourites/Bookmarks folder.
    Whenever you want to see the whois info of a website which you are
    browsing, just click on the bookmarklet & you'll get the WHOIS info,
    courtsey http://whois.webhosting.info

    Attached Files Attached Files
    Last edited by asp_funda; May 30, 2004 at 13:15.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  8. #8
    What a twist! Kings's Avatar
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    954
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works great in FireFox, good job!
    Dennis Pallett - NoCertainty - My Personal Weblog
    The Web Network: ASPit | PHPit | WebDev-Articles
    Blogs: TalkFones | Holidayzer | PHPit Blog

  9. #9
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just use location.host?
    Code:
    var re=/.*\.(.*\..*)$/;
    sld=re.exec(window.parent.location.host)[1];

  10. #10
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,482
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kings
    Works great in FireFox, good job!
    Thanks. I've tested it in IE6, Netscape7 & FireFox0.8
    Works ok. Don't know about other browsers but should
    work fine since no browser specific thing is used.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  11. #11
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,482
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Saturn
    Why not just use location.host?
    Code:
    var re=/.*\.(.*\..*)$/;
    sld=re.exec(window.parent.location.host)[1];
    Firstly it gives 2 values, one with a www subdomain
    added & one without it, though it can be fixed to
    extract the desired value & secondly, does it work in all
    browsers?
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!


Bookmarks

Posting Permissions

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