简体   繁体   English

JavaScript脚本在Firefox中不起作用

[英]JavaScript script doesn't work in Firefox

I have an old function which is missing lines for Mozilla/Firefox and thus JavaScript is not working properly in it. 我有一个旧功能,该功能缺少Mozilla / Firefox的行,因此JavaScript无法在其中正常运行。 The function tracks mouse-coordinates, so that I can position windows. 该功能跟踪鼠标坐标,以便我可以定位窗口。

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中可以正常工作。

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几乎是您所需要的,但让meg给您一些建议。

  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). StatCounter之类的网站咨询,以了解正在使用的浏览器(在您所在的国家/地区)。 Also consult with YUI graded browser support . 另请咨询YUI分级浏览器支持 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. 雅虎是互联网上最大的播放器之一,其网站必须为几乎每个人使用,因此他们知道自己在说什么。

  3. Find a good DOM reference. 找到一个好的DOM参考。 MDC is pretty much what you need, but it's good to have MSDN for IE quirks. MDC几乎是您所需要的,但是拥有适用于IE怪癖的MSDN很好。 Talking about quirks, don't forget to bookmark QuirksMode compatibility tables . 在谈论怪癖时,请不要忘记为QuirksMode兼容性表添加书签。

  4. Never use things like ie4 = document.all , because a single feature won't identify a whole browser. 切勿使用ie4 = document.all类的东西,因为单个功能无法识别整个浏览器。 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 阅读这两篇出色的文章: 浏览器检测(以及替代方法)功能检测:最新的浏览器脚本编写

  5. Don't use document.write because it's synchronous I/O which is awful. 不要使用document.write因为它是同步I / O,太糟糕了。 It blocks your page rendering and leads to bad user experience. 它会阻止页面渲染,并导致不良的用户体验。 The Web is all about being asynchronous . Web就是关于异步的

"Synchronous programming is disrespectful and should not be employed in applications which are used by people." “同步编程是不礼貌的,不应在人们使用的应用程序中使用。” - Douglas Crockford -道格拉斯·克罗克福德

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。

看来您需要将所有'event'实例更改为'e'。

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". 另请注意,由于您要传递给函数的参数是“ e”,因此我将“ event”更改为“ e”。 If you want to still use event, rewrite the parameter to: 如果您仍要使用事件,请将参数重写为:

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”。

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包括document.documentElementdocument.documentElement.scrollTop以及document.bodydocument.body.scrollTop因此您要输入用于Firefox 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而不是event中的所有使用场所event

添加var event = e的函数体的第一行,如果你害怕麻烦的function get_mouse(e) { var event = e;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM