为什么这个脚本在 Firefox 中不执行?

发布于 2024-08-15 06:07:10 字数 307 浏览 13 评论 0原文

我正在尝试运行一个非常简单的浏览器检测脚本,它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白况 2024-08-22 06:07:11

location="howdy.html" 更改为 location.href="howdy.html"

并且,停止进行浏览器嗅探。从长远来看,在 JavaScript 中进行功能检测可以打造更强大的应用程序。

Change location="howdy.html" to location.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.

情释 2024-08-22 06:07:11

您正在使用 break 语句错误的地方,我确信您遇到了语法错误,因为在循环或开关之外使用 break 是非法的。

ECMA-262 规范。参考:

12.8 break 语句

语法

BreakStatement :

break [no LineTerminator 此处] Identifieropt

语义

如果满足以下任一条件,程序将被视为语法不正确

  • 程序包含 break 语句,而没有可选的 Identifier,不直接或间接(但不跨越函数边界)嵌套在 IterationStatementSwitchStatement 中。

  • 该程序包含一个带有可选Identifierbreak语句,其中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 use break outside a loop or a switch.

ECMA-262 Spec. Reference:

12.8 The break Statement

Syntax

BreakStatement :

break [no LineTerminator here] Identifieropt ;

Semantics

A program is considered syntactically incorrect if either of the following is true:

  • The program contains a break statement without the optional Identifier, which is not nested, directly or indirectly (but not crossing function boundaries), within an IterationStatement or a SwitchStatement.

  • The program contains a break statement with the optional Identifier, where Identifier does not appear in the label set of an enclosing (but not crossing function boundaries) Statement.

瀟灑尐姊 2024-08-22 06:07:11

这只是一个次要问题,但它太大了,无法发表评论。

看看你的正则表达式,我认为那里有问题。 .* 匹配一切,并且由于默认情况下正则表达式是贪婪的,因此第一个 .* 将匹配字符串的整个其余部分,不留下任何内容有机会匹配 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 the Chrome part and forcing a failure. Some engines might be smart enough understand what you mean, but I would still simplify that to just /Chrome/.

紫瑟鸿黎 2024-08-22 06:07:11

如果是我,我会这样做

if(!/Chrome/.test(navigator.userAgent)) 
   location.href="howdy.html";

If me, I will do like this

if(!/Chrome/.test(navigator.userAgent)) 
   location.href="howdy.html";
鸢与 2024-08-22 06:07:11

我使用此代码来标记在多个浏览器上测试代码时记录的错误。

它为我在 IE、Safari、Firefox、Opera、Chrome 上测试的浏览器获取正确的浏览器名称和版本。但我称其为 navigator.sayswho,因为这就是导航器说的是谁。

navigator.sayswho= (function(){
 var N= navigator.appName, ua= navigator.userAgent, tem;
 var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
 if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
 M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
 return M;
})();

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.

navigator.sayswho= (function(){
 var N= navigator.appName, ua= navigator.userAgent, tem;
 var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
 if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
 M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
 return M;
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文