繁体   English   中英

javascript如何在FireFox中完美地工作,而在其他浏览器中却根本不工作?

[英]How can javascript work perfectly in FireFox, but not work at all in other browsers?

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

这个问题的根源是什么? 如果有什么不同,我将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错误后,您的代码将直接运行,或者导致错误的原因显而易见。

可能当您深入研究jQuery库时,您会发现针对其类和方法有特定于浏览器的实现。 我注意到它的AjaxManager。

也许您犯了我在这里谈论的错误: http : //my.opera.com/hallvors/blog/show.dml/26650在您的代码中的某个地方?

无论如何,要真正回答这个问题,我们需要链接到发生问题的整页。

暂无
暂无

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

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