簡體   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