用JavaScript确定域名? [英] Determine domain name in JavaScript?

查看:112
本文介绍了用JavaScript确定域名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

if (document.domain.toLowerCase().indexOf("domainName") != -1)

if(window.location.href.match(/:\/\/(.[^/]+)/)[1].toLowerCase().indexOf("domainName") != -1)

if(window.location.hostname.toLowerCase().indexOf("domainName") != -1)

我只是试图在某个域名上匹配,并希望使用最好的方法。

I'm just trying to match on a certain domainName and want to use the best approach.

推荐答案

最好和最可读的是:

if(location.hostname == "mysite.com"){

}






更新:

或者Patrick指出,如果您只是寻找域名的一部分,我将使用 match

Or as Patrick pointed out if you are only looking for part of the domain name I would use match.

if(location.hostname.match('mysite')){} // will return null if no match is found

这篇关于用JavaScript确定域名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆