简体   繁体   English

脚本在任何基于WebKit的浏览器中都可以正常运行,但在Firefox中却不能

[英]Script works fine in any WebKit based browser but doesn't in Firefox

I have a script in (using wordpress) header.php which looks like this: 我在(使用wordpress)header.php中有一个脚本,如下所示:

window.addEventListener("DOMContentLoaded", function(event) {
  fixedMenu.init("header-container", 0);
  fixedMenu.init("event-type-wrapper", 20);
  window.addEventListener('scroll', function(fid){
    fixedMenu.top("header-container");
    fixedMenu.top("event-type-wrapper");
  });
  var sessionvar = "<?php echo $_SESSION['lang'] ?>";
});

after this script I include a longer one script with the script scr="" tag. 在此脚本之后,我添加了一个较长的脚本,该脚本带有脚本scr =“”标签。

In that script I have 在那个剧本中

window.addEventListener("DOMContentLoaded", function(event) {
    todays = document.getElementById("todays-events");
    upcomming = document.getElementById("upcomming-events");
    past = document.getElementById("past-events");
    texter = document.getElementById("event-type");

    changeMessage();
});

It works fine in any webkit browser, the code is launched, but firefox doesn't load second event listener (I do not know how to debug if it loads or not, but I don't see result in the webpage). 它在任何webkit浏览器中都可以正常工作,可以启动代码,但是firefox不会加载第二个事件侦听器(我不知道如何调试是否加载了该事件,但是在网页中看不到结果)。

Any ideas? 有任何想法吗?

UPDATE UPDATE

If there is need, here is the full source for the script embedded through script src= 如果需要,这里是通过脚本src =嵌入的脚本的完整源代码。

var todays;
var upcomming;
var past;
var texter;

window.addEventListener("DOMContentLoaded", function(event) {
    todays = document.getElementById("todays-events");
    upcomming = document.getElementById("upcomming-events");
    past = document.getElementById("past-events");
    texter = document.getElementById("event-type");

    changeMessage();
});

window.onscroll = function(){
  changeMessage();
}

function isElementInViewport (el) {
  var rect = el.getBoundingClientRect();

  return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <= document.body.scrollHeight &&
    rect.right <= (window.innerWidth || document.documentElement.clientWidth)
  );
}

function changeMessage(){
  if(texter && todays && isElementInViewport(todays)){
    texter.textContent = returnString("Šiandien", "Today", "Сегодня");
  }
  else {
    if(texter && upcomming && isElementInViewport(upcomming)){
        texter.textContent = returnString("Artėjantys","Upcomming","Предстоящие");
    }
    else{
        if(texter && past && isElementInViewport(past)){
            texter.textContent = returnString("Praėję", "Past", "Прошлое");
        }
    }
  }
}

function returnString(lt, en, ru){
  if (!(typeof sessionvar === 'undefined')) {
    if(sessionvar == 'en_EN'){
        return en;
    }
    else if(sessionvar == 'ru_RU'){
        return ru;
    }
  }
  return lt;
}

The problem was that firefox reports 问题是firefox报告

document.body.scrollHeight

as 0, because of that every if inside changeMessage() failed by returning false. 为0,因为在changeMessage()内部的每个if changeMessage()失败,返回false。

The solution was found in SO: .body.scrollHeight doesn't work in Firefox 在SO中找到了解决方案: .body.scrollHeight在Firefox中不起作用

quote: 引用:

function getDocHeight() {
  var D = document;
  return Math.max(
    Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
    Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
    Math.max(D.body.clientHeight, D.documentElement.clientHeight)
  );
}

我认为那是行不通的是changeMes​​sage函数中的代码,因为Firefox无法理解element.innerText ,而您必须更改为element.textContent

暂无
暂无

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

相关问题 JavaScript在Firefox以外的任何浏览器中都能正常工作吗? - JavaScript works fine in any browser but Firefox? 页面在 Firefox 中无法正常工作,但在 chrome 中工作正常 - Page doesn't work as intended in Firefox but works fine in chrome Firefox无法正常播放动画gif(在Chrome中运行正常) - Firefox doesn't play animated gif properly (works fine in Chrome) Settimeout适用于基于chrome / webkit的浏览器,但不适用于firefox - Settimeout works in chrome/webkit based browsers but not firefox Facebook登录在浏览器中工作正常,但在带phonegap的android上不起作用 - Facebook login works fine in browser but doesn't work on android with phonegap Javascript语法在Safari / Webkit中无效,在Chrome / Firefox中正常运行 - Javascript syntax invalid in Safari / Webkit, works fine in Chrome / Firefox 基于Webkit的浏览器将json解释为脚本 - webkit based browser interpretate the json as a script 脚本仅适用于 Chrome 和 Firefox 桌面,不适用于 Safari 和任何移动浏览器 - Script works only on Chrome and Firefox Desktop, and not works on Safari and any mobile browser IE 11浏览器出错 - EXCEPTION:对象不支持属性或方法'匹配',其他浏览器工作正常 - Error in IE 11 browser - EXCEPTION: Object doesn't support property or method 'matches' , other browser it works fine 下拉菜单不会在Chrome中更新文本字段值,但在Firefox和IE中可以正常工作 - Dropdown Menu doesn't update textfield values in Chrome, but works fine in Firefox and IE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM