如何从 JavaScript 中的 URL 中提取 TLD

Mrk*_*abs 1 javascript google-chrome-extension

我想从 URL 中提取 TLD,但它给了我整个 URL。例如https://www.google.com.uk -> .com.uk。

$(document).on("click", "a", function() {
       var href = $(this).attr("href");
       alert(href)// It gives me the whole link of website
    if(href.search(".com.uk")){
        alert("This website is AUTHENTIC!");
    }
    else
    {
        alert("Not AUTHENTIC!")
    }
Run Code Online (Sandbox Code Playgroud)

Ove*_*der 7

要获取域的最后一部分,例如comin example.com,请使用以下内容:

const tld = window.location.origin.split('.').pop();
Run Code Online (Sandbox Code Playgroud)

但是,eTLD 之类的co.uk需要特殊情况。如果你想硬编码一个检查.co.uk

const isUK = window.location.origin.endsWith('.co.uk');
Run Code Online (Sandbox Code Playgroud)