【问题标题】:How can javascript work perfectly in FireFox, but not work at all in other browsers?javascript 如何在 FireFox 中完美运行,而在其他浏览器中却完全无法运行?
【发布时间】:2011-03-23 19:08:49
【问题描述】:

我使用 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);
}); 

【问题讨论】:

  • 给我们一些代码来处理......

标签: javascript firefox google-chrome opera


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    如果 typeof 为您的函数返回 undefined,则可能是 javascript 存在一些解析时错误。这意味着 firefox 对您的代码中的某些内容很宽容,而其他浏览器则不接受。

    我要做的是通过JSLint 传递代码以查看是否有任何错误。我在您的代码中看到了几个错误,但我不确定这是否是问题的原因。一旦 JSLint 错误得到修复,您的代码将直接运行,或者错误的原因很明显。

    【讨论】:

    • 谢谢!使用 JSLint 似乎可以修复代码,现在可以正常工作了。
    【解决方案3】:

    当您深入研究 jQuery 库时,您可能会发现它的类和方法有特定于浏览器的实现。我通过它的 AjaxManager 注意到了这一点。

    【讨论】:

      猜你喜欢
      • 2014-02-22
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 2015-09-15
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      相关资源
      最近更新 更多