JavaScript脚本在Firefox中不起作用。

时间:2021-03-15 01:14:27

I have an old function which is missing lines for Mozilla/Firefox and thus JavaScript is not working properly in it. The function tracks mouse-coordinates, so that I can position windows.

我有一个旧的函数,它丢失了Mozilla/Firefox的行,因此JavaScript不能正常工作。该函数跟踪鼠标坐标,以便我可以定位窗口。

How to make the code work in Firefox as well?

如何使代码在Firefox中工作?

Xoffset = -60; // modify these values to ...
Yoffset = 20; // change the popup position.
var old, skn, iex = (document.all),
    yyy = -1000;

var ns4 = document.layers
var ns6 = document.getElementById && !document.all
var ie4 = document.all

if (ns4) skn = document.dek
else if (ns6) skn = document.getElementById("dek").style
else if (ie4) skn = document.all.dek.style
if (ns4) document.captureEvents(Event.MOUSEMOVE);
else {
  skn.visibility = "visible"
  skn.display = "none"
}
document.onmousemove = get_mouse;


function popup(msg, bak) {
  var content = 
      "<TABLE  WIDTH=150 BORDER=1 BORDERCOLOR=black CELLPADDING=2" +
      "CELLSPACING=0 " + "BGCOLOR=" + bak + "><TD ALIGN=center>" + 
      "<FONT COLOR=black SIZE=2>" + msg + "</FONT></TD></TABLE>";
  yyy = Yoffset;
  if (ns4) {
    skn.document.write(content);
    skn.document.close();
    skn.visibility = "visible"
  }
  if (ns6) {
    document.getElementById("dek").innerHTML = content;
    skn.display = ''
  }
  if (ie4) {
    document.all("dek").innerHTML = content;
    skn.display = ''
  }
}

function get_mouse(e) {
  var x = (ns4 || ns6) ? event.pageX : event.x + document.body.scrollLeft;
  skn.left = x + Xoffset;
  var y = (ns4 || ns6) ? event.pageY : event.y + document.body.scrollTop;
  if (document.documentElement &&  // IE6 +4.01 but no scrolling going on
     !document.documentElement.scrollTop) {
    y = event.y + document.documentElement.scrollTop;
  }
  else if (document.documentElement && // IE6 +4.01 and user has scrolled
           document.documentElement.scrollTop) { 
    y = event.y + document.documentElement.scrollTop;
  }
  else if (document.body && document.body.scrollTop) { // IE5 or DTD 3.2
    y = event.y + document.document.body.scrollTop;
  }

  skn.top = y + yyy;
}

function kill() {
  yyy = -1000;
  if (ns4) {
    skn.visibility = "hidden";
  }
  else if (ns6 || ie4) skn.display = "none"
}

I am getting this error:

我得到了这个错误:

"event is not defined"

“事件没有定义”

Works ok in IE.

好的在IE中工作。

6 个解决方案

#1


2  

Instead of testing for browsers, I would test to see if the object / property exists. For example:

我将测试对象/属性是否存在,而不是测试浏览器。例如:

var x = e.pageX ? e.pageX : e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 

I think there may be an easier way to do this, such as

我认为可能有一种更简单的方法,例如

var x = e.pageX || e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;

but I'm not sure that will work. Test it out and see what you get. Also, for more detail, review: quirksmode.org/js/events_properties.html

但我不确定这是否行得通。测试一下,看看你得到了什么。此外,要了解更多细节,请查看:quirksmode.org/js/events_properties.html

Also note that I changed "event" to "e", since the parameter you're passing into the function is "e". If you want to still use event, rewrite the parameter to:

还要注意,我将“event”更改为“e”,因为您要传递给函数的参数是“e”。如果您还想使用event,将参数重写为:

function get_mouse(event)

While I don't believe "event" is a reserved word for JS, a lot of browsers use it, so I would suggest sticking to "e".

虽然我不认为“事件”是JS的专用词,但很多浏览器都使用它,所以我建议坚持使用“e”。

#2


7  

I'm not going to post code on how to rewrite your code @Ivo Wetzel's is pretty much what you need, but let meg give you some advice.

我不打算发布代码如何重写你的代码@Ivo Wetzel的代码是你需要的,但是让麦格给你一些建议。

  1. The world is changing fast, so does the computer industry. And while sometimes it's not as fast as we want (IE 6 fading slowly) there is no need to support Netscape 4.

    世界在快速变化,计算机产业也在变化。虽然有时候它并没有我们想要的那么快(IE 6慢慢衰落),但是没有必要支持Netscape 4。

  2. Consult with a site like StatCounter to find out what browsers are in use (in your country/region). Also consult with YUI graded browser support. Yahoo is one of the biggest players on the internet, their site has to work for almost everyone, so they know what they're talking about.

    向StatCounter这样的站点咨询,了解正在使用的浏览器(在您的国家/地区)。也可咨询YUI分级浏览器支持。雅虎是互联网上最大的玩家之一,他们的网站必须为几乎所有人服务,所以他们知道自己在说什么。

  3. Find a good DOM reference. MDC is pretty much what you need, but it's good to have MSDN for IE quirks. Talking about quirks, don't forget to bookmark QuirksMode compatibility tables.

    找到一个好的DOM引用。MDC差不多就是您所需要的,但是对于IE怪癖有MSDN是很好的。说到怪癖,别忘了加书签。

  4. Never use things like ie4 = document.all, because a single feature won't identify a whole browser. It's like saying: "Hey you've got blonde hair, you must be Brad Pitt". Use feature detection. Read these two excellent articles: Browser Detection (and What to Do Instead) and Feature Detection: State of the Art Browser Scripting

    不要使用像ie4 = document这样的东西。因为单个功能不能识别整个浏览器。就像在说:“嘿,你有一头金发,你一定是布拉德·皮特。”使用特征检测。阅读这两篇优秀的文章:浏览器检测(以及应该做什么)和特性检测:最新的浏览器脚本

  5. Don't use document.write because it's synchronous I/O which is awful. It blocks your page rendering and leads to bad user experience. The Web is all about being asynchronous.

    不要使用文档。写因为它是同步的I/O,这很糟糕。它会阻止你的页面呈现,并导致糟糕的用户体验。网络是关于异步的。

"Synchronous programming is disrespectful and should not be employed in applications which are used by people." - Douglas Crockford

“同步编程是不礼貌的,不应该在人们使用的应用程序中使用。”——道格拉斯Crockford

#3


3  

Oh my god... this must be the worst code I've seen in years, well let's try to clean it up then:

哦我的上帝…这肯定是我多年来见过的最糟糕的代码了,我们试着把它清理一下吧:

Xoffset = -60; // modify these values to ...
Yoffset = 20; // change the popup position.
var old, skn = document.getElementById("dek").style, yyy = -1000;

function popup(msg, bak) {
    var content = 
        "<TABLE  WIDTH=150 BORDER=1 BORDERCOLOR=black CELLPADDING=2" +
        "CELLSPACING=0 " + "BGCOLOR=" + bak + "><TD ALIGN=center>" + 
        "<FONT COLOR=black SIZE=2>" + msg + "</FONT></TD></TABLE>";

    yyy = Yoffset;
    document.getElementById("dek").innerHTML = content;
    skn.display = '';
}

document.onmousemove = function(e) {
    e = e || window.event;

    var x = e.pageX !== undefined ? e.pageX : e.clientX + document.body.scrollLeft;
    var y = e.pageY !== undefined ? e.pageY : e.clientY + document.body.scrollTop;
    skn.left = x + Xoffset;
    skn.top = y + yyy;
}

function kill() {
  yyy = -1000;
  skn.display = "none";
}

It's still broken beyond repair, but it should work... somehow.... Well, unless you post the rest of them HTML there's no way I can test this.

它还坏得无法修理,但应该能修好……不知何故....好吧,除非你把剩下的都发布到HTML上否则我没法测试这个。

Please, I beg you... get rid of all that crap and use jQuery.

请,我请求你……摆脱那些废话,使用jQuery。

#4


2  

Looks like you need to change all your instances of 'event' to 'e'.

看起来您需要将“event”的所有实例更改为“e”。

#5


1  

Firefox includes document.documentElement and document.documentElement.scrollTop and document.body and document.body.scrollTop so you're entering regions that were meant for IE with Firefox.

Firefox包括文档。documentElement document.documentElement。scrollTop和文档。身体和document.body。滚动桌面,这样你就进入了IE和火狐的区域。

You should also start your function with something like

你也应该从类似的东西开始你的函数

function get_mouse(e) {
    e = e || window.event;

Then use e instead of event in all the places you use event.

然后在你使用的所有地方使用e而不是事件。

#6


0  

Add var event = e on the first line of function body, if you are afraid of hassles function get_mouse(e) { var event = e;

如果您害怕函数get_mouse(e) {var event = e;

#1


2  

Instead of testing for browsers, I would test to see if the object / property exists. For example:

我将测试对象/属性是否存在,而不是测试浏览器。例如:

var x = e.pageX ? e.pageX : e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 

I think there may be an easier way to do this, such as

我认为可能有一种更简单的方法,例如

var x = e.pageX || e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;

but I'm not sure that will work. Test it out and see what you get. Also, for more detail, review: quirksmode.org/js/events_properties.html

但我不确定这是否行得通。测试一下,看看你得到了什么。此外,要了解更多细节,请查看:quirksmode.org/js/events_properties.html

Also note that I changed "event" to "e", since the parameter you're passing into the function is "e". If you want to still use event, rewrite the parameter to:

还要注意,我将“event”更改为“e”,因为您要传递给函数的参数是“e”。如果您还想使用event,将参数重写为:

function get_mouse(event)

While I don't believe "event" is a reserved word for JS, a lot of browsers use it, so I would suggest sticking to "e".

虽然我不认为“事件”是JS的专用词,但很多浏览器都使用它,所以我建议坚持使用“e”。

#2


7  

I'm not going to post code on how to rewrite your code @Ivo Wetzel's is pretty much what you need, but let meg give you some advice.

我不打算发布代码如何重写你的代码@Ivo Wetzel的代码是你需要的,但是让麦格给你一些建议。

  1. The world is changing fast, so does the computer industry. And while sometimes it's not as fast as we want (IE 6 fading slowly) there is no need to support Netscape 4.

    世界在快速变化,计算机产业也在变化。虽然有时候它并没有我们想要的那么快(IE 6慢慢衰落),但是没有必要支持Netscape 4。

  2. Consult with a site like StatCounter to find out what browsers are in use (in your country/region). Also consult with YUI graded browser support. Yahoo is one of the biggest players on the internet, their site has to work for almost everyone, so they know what they're talking about.

    向StatCounter这样的站点咨询,了解正在使用的浏览器(在您的国家/地区)。也可咨询YUI分级浏览器支持。雅虎是互联网上最大的玩家之一,他们的网站必须为几乎所有人服务,所以他们知道自己在说什么。

  3. Find a good DOM reference. MDC is pretty much what you need, but it's good to have MSDN for IE quirks. Talking about quirks, don't forget to bookmark QuirksMode compatibility tables.

    找到一个好的DOM引用。MDC差不多就是您所需要的,但是对于IE怪癖有MSDN是很好的。说到怪癖,别忘了加书签。

  4. Never use things like ie4 = document.all, because a single feature won't identify a whole browser. It's like saying: "Hey you've got blonde hair, you must be Brad Pitt". Use feature detection. Read these two excellent articles: Browser Detection (and What to Do Instead) and Feature Detection: State of the Art Browser Scripting

    不要使用像ie4 = document这样的东西。因为单个功能不能识别整个浏览器。就像在说:“嘿,你有一头金发,你一定是布拉德·皮特。”使用特征检测。阅读这两篇优秀的文章:浏览器检测(以及应该做什么)和特性检测:最新的浏览器脚本

  5. Don't use document.write because it's synchronous I/O which is awful. It blocks your page rendering and leads to bad user experience. The Web is all about being asynchronous.

    不要使用文档。写因为它是同步的I/O,这很糟糕。它会阻止你的页面呈现,并导致糟糕的用户体验。网络是关于异步的。

"Synchronous programming is disrespectful and should not be employed in applications which are used by people." - Douglas Crockford

“同步编程是不礼貌的,不应该在人们使用的应用程序中使用。”——道格拉斯Crockford

#3


3  

Oh my god... this must be the worst code I've seen in years, well let's try to clean it up then:

哦我的上帝…这肯定是我多年来见过的最糟糕的代码了,我们试着把它清理一下吧:

Xoffset = -60; // modify these values to ...
Yoffset = 20; // change the popup position.
var old, skn = document.getElementById("dek").style, yyy = -1000;

function popup(msg, bak) {
    var content = 
        "<TABLE  WIDTH=150 BORDER=1 BORDERCOLOR=black CELLPADDING=2" +
        "CELLSPACING=0 " + "BGCOLOR=" + bak + "><TD ALIGN=center>" + 
        "<FONT COLOR=black SIZE=2>" + msg + "</FONT></TD></TABLE>";

    yyy = Yoffset;
    document.getElementById("dek").innerHTML = content;
    skn.display = '';
}

document.onmousemove = function(e) {
    e = e || window.event;

    var x = e.pageX !== undefined ? e.pageX : e.clientX + document.body.scrollLeft;
    var y = e.pageY !== undefined ? e.pageY : e.clientY + document.body.scrollTop;
    skn.left = x + Xoffset;
    skn.top = y + yyy;
}

function kill() {
  yyy = -1000;
  skn.display = "none";
}

It's still broken beyond repair, but it should work... somehow.... Well, unless you post the rest of them HTML there's no way I can test this.

它还坏得无法修理,但应该能修好……不知何故....好吧,除非你把剩下的都发布到HTML上否则我没法测试这个。

Please, I beg you... get rid of all that crap and use jQuery.

请,我请求你……摆脱那些废话,使用jQuery。

#4


2  

Looks like you need to change all your instances of 'event' to 'e'.

看起来您需要将“event”的所有实例更改为“e”。

#5


1  

Firefox includes document.documentElement and document.documentElement.scrollTop and document.body and document.body.scrollTop so you're entering regions that were meant for IE with Firefox.

Firefox包括文档。documentElement document.documentElement。scrollTop和文档。身体和document.body。滚动桌面,这样你就进入了IE和火狐的区域。

You should also start your function with something like

你也应该从类似的东西开始你的函数

function get_mouse(e) {
    e = e || window.event;

Then use e instead of event in all the places you use event.

然后在你使用的所有地方使用e而不是事件。

#6


0  

Add var event = e on the first line of function body, if you are afraid of hassles function get_mouse(e) { var event = e;

如果您害怕函数get_mouse(e) {var event = e;