如何在FireFox中完美地运行javascript,但在其他浏览器中却无法正常工作?

时间:2010-08-01 06:11:15

标签: 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);
}); 

3 个答案:

答案 0 :(得分:2)

如果typeof返回未定义的函数,则javascript可能存在一些解析时错误。这意味着firefox对你的代码接受是宽容的,其他浏览器则没有。

我要做的是通过JSLint传递代码,看看是否有任何错误。我在你的代码中看到了几个错误,但我不确定它是否是导致问题的原因。一旦修复了JSLint错误,您的代码将直接工作,或者错误的原因将是显而易见的。

答案 1 :(得分:0)

当你深入研究jQuery库时,你会发现它的类和方法都有浏览器特定的实现。我用它的AjaxManager注意到了这一点。

答案 2 :(得分:0)

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

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

相关问题