You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firefox中如何从Controller调用JavaScript?函数未定义错误

解决Firefox中Bean调用JS函数时的ReferenceError问题

你遇到的核心问题是函数作用域限制:funkcjaKolorujPredkosc被定义在$(document).ready()的回调函数内部,该函数的作用域仅局限于ready回调的局部环境中。Firefox严格遵循JavaScript作用域规则,无法在全局环境下找到该函数;而Edge可能存在非标准的作用域兼容行为,因此能正常执行。

以下是两种可行的解决方案:

方案1:将函数定义暴露到全局作用域

修改KolorSpeed.js代码,把函数定义移到$(document).ready()外部,同时在ready回调中初始化执行:

// 定义全局可访问的函数
function funkcjaKolorujPredkosc(){
    var allCells = $(".kolorSpeed > div");

    $.each(allCells, function (index, child) {
        var speedString = (child.textContent.replace("/","-")).toString();
        var index = speedString.indexOf("-");
        var actualSpeed = parseInt(speedString.substring(0, index)); 
        var targetSpeed = parseInt(speedString.substring(index+1, speedString.length));        

        if (actualSpeed >= targetSpeed){
            $(child).parent().css("color", "green");
        } else {
            $(child).parent().css("color", "red");
        }
    }); 
}

// 页面加载完成后自动执行一次
$(document).ready(function(){
    funkcjaKolorujPredkosc();
});

方案2:在ready回调中挂载函数到window全局对象

如果不想调整函数定义位置,可在ready回调内将函数赋值给window对象,使其成为全局属性:

$(document).ready(function(){
    function funkcjaKolorujPredkosc(){
        var allCells = $(".kolorSpeed > div");

        $.each(allCells, function (index, child) {
            var speedString = (child.textContent.replace("/","-")).toString();
            var index = speedString.indexOf("-");
            var actualSpeed = parseInt(speedString.substring(0, index)); 
            var targetSpeed = parseInt(speedString.substring(index+1, speedString.length));        

            if (actualSpeed >= targetSpeed){
                $(child).parent().css("color", "green");
            } else {
                $(child).parent().css("color", "red");
            }
        }); 
    }

    // 挂载到全局window对象,允许外部调用
    window.funkcjaKolorujPredkosc = funkcjaKolorujPredkosc;
    // 页面加载完成后执行一次
    funkcjaKolorujPredkosc();
});

额外验证步骤

  • 打开Firefox开发者工具的「网络」面板,确认KolorSpeed.js脚本已成功加载,路径无错误
  • 清理Firefox浏览器缓存,避免旧脚本缓存导致的异常

内容的提问来源于stack exchange,提问作者Eiten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 05:03:21