Coffeescript 添加:咖啡脚本中URL的外部
我有一点coffeescript,我正在使用它向我网站上的所有外部链接添加一个Coffeescript 添加:咖啡脚本中URL的外部,coffeescript,Coffeescript,我有一点coffeescript,我正在使用它向我网站上的所有外部链接添加一个:external伪类 jQuery.expr[":"].external = (obj) -> obj.hostname isnt location.hostname 我想做的是允许例外,例如 jQuery.expr[":"].external = (obj) -> obj.hostname isnt location.hostname unless obj.hostname is "domai
:external
伪类
jQuery.expr[":"].external = (obj) ->
obj.hostname isnt location.hostname
我想做的是允许例外,例如
jQuery.expr[":"].external = (obj) ->
obj.hostname isnt location.hostname unless obj.hostname is "domain.com" or "sub.domain.com"
然而,这是行不通的 首先,
x是a或b
不测试x
是a
还是b
;您正在查找x是a或x是b
。这也可以写为[a,b]中的x,无论如何编译成相同的东西
其次,后缀,除非有点不寻常<代码>(a)->b,除非c编译为
function (a) {
if (!c) {
return b;
}
}
因此,如果c
为true,则函数返回undefined
,这是false。这会管用的,但我会觉得很困惑。其逻辑实际上是:只要链接目的地不是location.hostname
,“domain.com”或“sub.domain.com”,即
可以更简洁地写为
obj.hostname not in [location.hostname, "domain.com", "sub.domain.com"]
注意,is还将把伪类应用于obj.hostname
是未定义的
,因为未定义的
肯定不是location.hostname
。这可能不是你想要的。您可以使用obj.hostname吗?和obj.hostname不在[…]
中,以过滤掉根本没有主机名的元素。我不确定这个jQuery和外部内容,但是如果第一个示例起作用,那么如果希望显示为外部链接,则只应返回true,因此在您的情况下:
jQuery.expr[':'].external = (obj) ->
obj.hostname not in [location.hostname, 'domain.com', 'sub.domain.com']
最后,我想要的是允许我的特定域的所有子域作为例外。所以-将链接分类为外部链接,但如果链接HREF为空,它们将处理js事件,或者指向domain.com的子域,则不会将其视为外部链接
$.expr[":"].external = (obj) ->
h = obj.href
typeof h isnt 'undefined' and h.indexOf('#') is -1 and h.indexOf('javascript') is -1 and h.indexOf('domain.com') is -1
还有一个问题需要跟进-我可以设置一个排除所有.domain.com子域的规则吗?e、 g.*.domain.com-是否不全部指定?
$.expr[":"].external = (obj) ->
h = obj.href
typeof h isnt 'undefined' and h.indexOf('#') is -1 and h.indexOf('javascript') is -1 and h.indexOf('domain.com') is -1