如何从JavaScript中的URL提取TLD [英] How to extract the TLD from the URL in JavaScript
本文介绍了如何从JavaScript中的URL提取TLD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从URL中提取TLD,但它却给了我整个URL. 例如 https://www.google.com.uk -> .com.uk.
I want to extract the TLD from the URL but it's giving me the whole URL instead. For example 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
,请使用类似以下内容的东西:
To get the last piece of a domain, such as com
in example.com
, use something like:
const tld = window.location.origin.split('.').pop();
但是,像co.uk
这样的eTLD需要特殊情况.如果您要对.co.uk
的支票进行硬编码:
However, eTLDs like co.uk
need special cases. If you want to hardcode a check for .co.uk
:
const isUK = window.location.origin.endsWith('.co.uk');
这篇关于如何从JavaScript中的URL提取TLD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文