Issue while capturing Top-Level Domain from URL

坚强是说给别人听的谎言 提交于 2019-12-22 14:00:12

问题


I want a way to capture the Top-Level Domain from a URL, but am not able to get any success. The problem in my case is that the URL can be different. Sometimes a user can enter www.google.com or m.google.com or m.google.uk or google.uk or www.m.google.com

I tried using slice but it didn't work as I can have 2 or 3 characters in my URL. I can't split based on ".", I might get 2 or 3 or 4 results. Is there a single-line JavaScript function I can use? Is there any easy custom function available?

All posts are pointing to get the host name but in my case I want to extract just last 3 or 2 characters of the URL (com, uk, cn, etc.). I can apply multiple if-else loops too but I want to avoid that, and want to check if there is a simple solution for this.

I am looking for output as 'com' or 'uk' or 'cn' depending on top level domain of my URL. URL is entered by user which is why it difficult to predict if user will enter m.google.com or www.m.google.com or www.google.com or simply google.com


回答1:


One possible approach:

var parser = document.createElement('a');

parser.href = "http://www.google.com/path/";
console.log(parser.hostname); // "www.google.com"

parser.href = "http://m.google.com/path/";
console.log(parser.hostname); // "m.google.com"

parser.href = "http://www.m.google.com/path/";
console.log(parser.hostname); // "www.m.google.com"



回答2:


Below code works for me. Thanks @StephenP for your help. Thanks @Timo as well but it seems Document is not identified in protractor library.

var parser = TextBox.siteName;//get input of site from user in parser variable.
 var hostParts = parser.split('.');
    var URLdomain = hostParts[hostParts.length - 1];



回答3:


If you can isolate the domain, the last period (.) should signify the TLD.

Test it out here: https://jsfiddle.net/ubb61wam/2/

var addresses = [
  'google.com',             // should return 'com'
  'https://google.com.uk',  // should return 'uk'
  'yahoo.cn/foo/bar.foo',   // should return 'cn'
  'file:///usr/local'       // should fail
];

for (var index in addresses) {
    console.log(tld(addresses[index]));
}

function tld(address) {
    // handle edge-cases
    if (typeof address == 'undefined' || address.indexOf('file:///') != -1)
        return undefined;

    var part = address;

    //remove http://
    if (part.indexOf('//') != -1)
        part = part.split('//')[1];

    //isolate domain
    if (part.indexOf('/') != -1)
        part = part.split('/')[0];  

    //get tld
    if (part.indexOf('.') != -1) {
        var all = part.split('.');
        part = all[all.length - 1]; 
    }
    return part;
}


来源:https://stackoverflow.com/questions/40428687/issue-while-capturing-top-level-domain-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!