javascript - javascript 如何在 FireFox 中完美工作,但在其他浏览器中根本无法工作?

标签 javascript firefox google-chrome opera

我使用 FireFox 作为我的主要浏览器,特别是在测试我的网站时,Avoru 。然而,当检查我的代码是否在其他主要浏览器(Google Chrome、Opera 和 Safari)上正常工作时,我发现我的自定义 JavaScript 似乎都不起作用。尽管页面源代码中的函数和代码很清楚,但使用 typeof 为我的所有函数返回了“未定义”值。

这个问题的根源是什么?如果有什么区别的话,我在代码中使用 Prototype 和 jQuery 库,并在页面底部加载所有 javascript(出于速度原因)。谢谢!

编辑:这是一些代码。

// === var $j frees up the $ selector. === //
var $j = jQuery.noConflict();
// === Function: loading(); and loaded(); Manually controls the #loading element. === //
function loading(){
    $j('#load_ovrly').css({'display':'block'});
    $j('#loader').fadeTo('fast',1);
}
function loaded(){
    $j('#load_ovrly').css({'display':'none'});
    $j('#loader').fadeTo('fast',.0001);
}
// === Function: content(); Using everything after the #, the hash is processed and requested. === //
function content(theHash){
    var hashIndex = theHash.indexOf('-');
    var commaIndex = theHash.indexOf(',');
    // === Split the Hash accordingly. === //
    if((hashIndex > commaIndex) || (commaIndex == -1 && hashIndex == -1)) newHash = theHash.split(',');
    if((commaIndex > hashIndex) || (commaIndex == -1 && hashIndex != -1)) newHash = theHash.split('-');
    // === Set some extra variables for proofing. === //
    var url = newHash[0]+".php";
    // === Get parameters if there are any. === //
    if(newHash[1]){
        var Json = jsonify(newHash[1]);
        var pars = "p="+Json;
    }else{
        var pars = "p={\"forcepars\":\"true\"}";
    }   
    // === Finally request the page. === //
    request(url,pars);
}
// === Function: jsonify(); Turns the leftover hash from content(); into valid JSON. === //
function jsonify(str){
    var Json = "{";
    var split = str.split(",");
    for(var a = 0; a < split.length; a++){
        if(a > 0){Json = Json+",";}
        var b = split[a].split(":");
        if(b[1] != undefined) Json = Json+"\""+b[0]+"\":\""+b[1]+"\"";
    }
    return Json+"}";
}
// === Function: AJAX(); Sends an ajax request given the url, some parameters, and the onComplete. === //
function AJAX(url,parameters,complete){
    $j.ajax({
        type: 'POST',
        url: url,
        data: parameters,
        complete: function($data){
            var data = $data.responseText;
            complete(data);
        }
    });
}
// === Function: request(); Takes the properly formatted url and parameters and requests the page. === //
function request(url,parameters){
    AJAX(url,parameters,
        function(data){
            $j('#my_box').html(data);
        }   
    );      
}
// === Function: sendForm(); Sends the form and updates the page. === //
function sendForm(id,url){
    var form = $j("form#"+id).serialize();
    AJAX(url,form,function(data){$j("#my_box").html(data);});   
}
// === Below are items that are activated once the DOM is loaded. === //
var curHashVal = window.location.hash;
document.observe("dom:loaded",function(){   
    $j("#loader").ajaxStart(function(){
        loading();
    }).ajaxStop(function(){
        loaded();
    }); 
    if(window.location.hash.length > 1) content(curHashVal.substr(1));
    new PeriodicalExecuter(function() {
        if(curHashVal != window.location.hash){
            content(window.location.hash.substr(1));
            curHashVal = window.location.hash;
        }
    },.15);
}); 

最佳答案

如果 typeof 返回的函数未定义,则很可能存在 JavaScript 的解析时错误。这意味着 Firefox 在您的代码中对接受的内容很宽松,而其他浏览器则不然。

我要做的是将代码传递给 JSLint看看是否有任何错误。我在您的代码中看到了几个错误,但我不确定这是否是问题的原因。一旦 JSLint 错误被修复,您的代码要么直接工作,要么错误的原因将很明显。

关于javascript - javascript 如何在 FireFox 中完美工作,但在其他浏览器中根本无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3380965/

相关文章:

Javascript 获取文件名不起作用

css - 悬停时的 PNG 阴影在 Firefox 中不起作用-我做错了什么?

html - 鼠标移到该区域时不开始过渡

javascript - 与 requirejs 连接并排除单个文件?

javascript - 文档准备好后使用选择器的问题

javascript - jquery ajax 调用 "metho"而不是 "method"?

javascript - DOM 更改事件 Firefox 扩展

css - PHP/HTML/CSS - 如果是 FireFox,是 Chrome 还是 Safari

javascript - 如何为 chrome 上的每个标签获得不同的徽章值?

javascript - background.js 中的 Chrome 扩展 keydown 监听器