为什么这个脚本在 Firefox 中不执行?
我正在尝试运行一个非常简单的浏览器检测脚本,它在 Safari 和 Chrome(在 Mac 上运行)中执行得很好,但在 Firefox 中根本不执行。我可以将其归结为最简单的形式,但它仍然无法执行:
<script type="text/javascript">
if (navigator.userAgent.match(/^.*Chrome.*$/)) {break;}
else {
location="howdy.html"
}
</script>
这已经让我困惑了几个小时。有人有主意吗?谢谢!!
I'm trying to run a very simple browser detect script and it executes just fine in Safari and Chrome (running on a Mac) but doesn't execute at all in Firefox. I can boil it down to the simplest possible form, and it still doesn't execute:
<script type="text/javascript">
if (navigator.userAgent.match(/^.*Chrome.*$/)) {break;}
else {
location="howdy.html"
}
</script>
This has been perplexing me for hours now. Anyone have an idea? Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将
location="howdy.html"
更改为location.href="howdy.html"
并且,停止进行浏览器嗅探。从长远来看,在 JavaScript 中进行功能检测可以打造更强大的应用程序。
Change
location="howdy.html"
tolocation.href="howdy.html"
And also, stop doing browser-sniffing. Do feature detection in your JavaScript to make a more robust application in the long-term.
您正在使用
break
语句错误的地方,我确信您遇到了语法错误,因为在循环或开关之外使用break
是非法的。ECMA-262 规范。参考:
12.8
break
语句语法
BreakStatement
:break
[noLineTerminator
此处]Identifieropt
;语义
如果满足以下任一条件,程序将被视为语法不正确:
程序包含
break
语句,而没有可选的Identifier
,不直接或间接(但不跨越函数边界)嵌套在IterationStatement
或SwitchStatement
中。该程序包含一个带有可选
Identifier
的break
语句,其中Identifier
不出现在封闭的标签集中(但不跨越函数边界)语句
。You are using the
break
statement in a wrong place, I'm sure you are getting a syntax error, since it's illegal to usebreak
outside a loop or a switch.ECMA-262 Spec. Reference:
12.8 The
break
StatementSyntax
BreakStatement
:break
[noLineTerminator
here]Identifieropt
;Semantics
A program is considered syntactically incorrect if either of the following is true:
The program contains a
break
statement without the optionalIdentifier
, which is not nested, directly or indirectly (but not crossing function boundaries), within anIterationStatement
or aSwitchStatement
.The program contains a
break
statement with the optionalIdentifier
, whereIdentifier
does not appear in the label set of an enclosing (but not crossing function boundaries)Statement
.这只是一个次要问题,但它太大了,无法发表评论。
看看你的正则表达式,我认为那里有问题。
.*
匹配一切,并且由于默认情况下正则表达式是贪婪的,因此第一个.*
将匹配字符串的整个其余部分,不留下任何内容有机会匹配Chrome
部分并强制失败。有些引擎可能足够聪明,可以理解您的意思,但我仍然会将其简化为/Chrome/
。This is just a side issue, but it's too big for a comment.
Looking at your regex, I have to think there's something wrong there.
.*
matches everything, and since regular expressions are greedy by default the first.*
would match on the entire rest of the string, leaving no chance to match theChrome
part and forcing a failure. Some engines might be smart enough understand what you mean, but I would still simplify that to just/Chrome/
.如果是我,我会这样做
If me, I will do like this
我使用此代码来标记在多个浏览器上测试代码时记录的错误。
它为我在 IE、Safari、Firefox、Opera、Chrome 上测试的浏览器获取正确的浏览器名称和版本。但我称其为 navigator.sayswho,因为这就是导航器说的是谁。
I use this code to tag errors logged when I'm testing code on multiple browsers.
It gets the browser name and version right for the browsers I test on-IE, Safari, Firefox, Opera, Chrome. But I call it navigator.sayswho because that's all it is- who the navigator says it is.