Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Coffeescript 添加:咖啡脚本中URL的外部_Coffeescript - Fatal编程技术网

Coffeescript 添加:咖啡脚本中URL的外部

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

我有一点coffeescript,我正在使用它向我网站上的所有外部链接添加一个
: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