gpt4 book ai didi

javascript - 如何在 JavaScript 中从 URL 中提取 TLD

转载 作者:行者123 更新时间:2023-11-28 12:16:40 25 4
gpt4 key购买 nike

我想从 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!")
}

最佳答案

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

const tld = window.location.origin.split('.').pop();

但是,像 co.uk 这样的 eTLD 需要特殊情况。如果您想对 .co.uk 进行硬编码检查:

const isUK = window.location.origin.endsWith('.co.uk');

关于javascript - 如何在 JavaScript 中从 URL 中提取 TLD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48060246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com