IE8 奇怪的行为,我的问题还是 bug?

发布于 2024-10-04 07:12:29 字数 1881 浏览 0 评论 0原文

好的,伙计们,这是 intreting,

我正在测试此页面

http://static.nemesisdesign .net/demos/ie8-strange/test.html

在 IE8 / Windows XP 上。

这是代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="content-language" content="en">
    <title>Test</title>
</head>
<body>
<div id="thisgivesmeanerror">test</div>
<script>
    thisgivesmeanerror = 'test';
    alert('this alert won\'t be fired on my IE8, what about yours?');
</script>
</body>
</html>

如果我用 IE8 打开此页面,则会出现错误。 如果我将脚本的代码更改为:

<script>
    // note that I added var prefix
    var thisgivesmeanerror = 'test';
    alert('this alert won\'t be fired on my IE8, what about yours?');
</script>

它工作正常。 这只发生在 IE 7/8 上,没有在 IE6 上测试。

你怎么认为? 你也会遇到这种情况吗?或者只是我的浏览器发疯了?

另外

你是说只是没有使用 var 前缀导致错误? 很抱歉大家,但你们错了,你们没有花时间测试代码。

我上传了一个test2页面 http://static.nemesisdesign.net/demos/ie8-strange/test2.html 使用以下代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="content-language" content="en">
    <title>Test 2</title>
</head>
<body>
<div id="thisgivesmeanerror">test</div>
<script>
    thisdoesntgiveanyerror = 'test';
    alert('This alert will be fired correcly');
</script>
</body>
</html>

效果很好。

那么到底是什么导致了这个错误呢?不带 var 前缀的变量名与 DIV 元素的 ID 具有相同的名称。

这不奇怪吗?

OK guys this is intreting,

I'm testing this page

http://static.nemesisdesign.net/demos/ie8-strange/test.html

on IE8 / windows XP.

This is the code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="content-language" content="en">
    <title>Test</title>
</head>
<body>
<div id="thisgivesmeanerror">test</div>
<script>
    thisgivesmeanerror = 'test';
    alert('this alert won\'t be fired on my IE8, what about yours?');
</script>
</body>
</html>

If I open this page with IE8 I get an error.
If I change the code of the script to:

<script>
    // note that I added var prefix
    var thisgivesmeanerror = 'test';
    alert('this alert won\'t be fired on my IE8, what about yours?');
</script>

It works fine.
This happens only on IE 7/8, didn't test it on IE6.

What do you think?
Does it happen to you also? Or is it just my browser that has gone crazy?

Addition

You're saying that is just not using the var prefix that cause the error?
I'm sorry guys but you're wrong, you didn't take time to test the code.

I uploaded a test2 page
http://static.nemesisdesign.net/demos/ie8-strange/test2.html
with the follwing cocde

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="content-language" content="en">
    <title>Test 2</title>
</head>
<body>
<div id="thisgivesmeanerror">test</div>
<script>
    thisdoesntgiveanyerror = 'test';
    alert('This alert will be fired correcly');
</script>
</body>
</html>

That works fine.

So what is actually causing the error? The variable name without the var prefix having the same name as the ID of the DIV element.

Isn't this strange?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

陪我终i 2024-10-11 07:12:30

可能是这个问题的答案可以帮助?

May be the answers to this SO-question can help?

〃温暖了心ぐ 2024-10-11 07:12:30

您应该始终在变量声明之前添加 var 以指定其范围,否则您可能会发现不同浏览器之间的行为不一致。

You should always precede your variable declarations with var to specify their scope or you might observe inconsistent behavior between different browsers.

乖不如嘢 2024-10-11 07:12:30

使用 var 声明变量,而不仅仅是插入变量的名称

use var to declare variables instead of just plugging their name

深巷少女 2024-10-11 07:12:30

我想说 IE 中的 JavaScript 解释器比 FireFox 和其他浏览器中的 JavaScript 解释器稍微严格一些,这意味着脚本在涉及变量定义行时会返回错误。将 var 放入将确保它实际上一个变量。

使用 var 声明所有变量是非常好的做法

James

EDIT

我目前无法访问 IE,但我可以建议您更改 < script> 标记为

I'd say the JavaScript interpreter in IE is slightly stricter than on FireFox and others, meaning the script returns an error when it comes to the variable definition line. Putting var in will ensure it actually is a variable.

It's very good practice to declare all your variables with var

James

EDIT

I can't get to IE at the moment, but I can recommend you change your <script> tag to <script type="text/javascript">.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文