简体   繁体   English

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

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

I use FireFox as my main browser, especially when testing out my site, Avoru . 我使用FireFox作为主要浏览器,尤其是在测试我的网站Avoru时 However, when checking to see if my code was working properly across other major browsers (Google Chrome, Opera, and Safari), I found that none of my custom javascript seemed to work. 但是,当检查我的代码在其他主要浏览器(Google Chrome,Opera和Safari)上是否正常运行时,我发现自定义javascript似乎都不起作用。 Although the functions and code were clear in the page source, using typeof returned an 'undefined' value for all my functions. 尽管页面源代码中的功能和代码很清楚,但是使用typeof返回的所有函数的值都为“ undefined”。

What is the root of this problem? 这个问题的根源是什么? If it makes any difference, I use both the Prototype and jQuery libraries for my code, as well as load all javascript at the bottom of the page (for speed reasons). 如果有什么不同,我将Prototype和jQuery库用于我的代码,并将所有JavaScript加载到页面底部(出于速度原因)。 Thanks! 谢谢!

EDIT: Here is some of the code. 编辑:这是一些代码。

// === 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);
}); 

If typeof returned undefined for your functions, chances are there was some parse-time error of the javascript. 如果您的函数的typeof返回未定义,则可能是JavaScript出现了解析时错误。 This means that something that firefox was lenient about accepting in your code, other browsers weren't. 这意味着firefox宽容地接受您的代码,而其他浏览器则不然。

What I'd do is pass the code through JSLint to see if there are any errors. 我要做的是通过JSLint传递代码,看是否有任何错误。 I saw several errors in your code, but I'm not sure if it would be the cause of the problem. 我在您的代码中看到了几个错误,但是我不确定这是否是问题的原因。 Once the JSLint errors are fixed, your code will either work directly, or the cause of the error will be obvious. 修复JSLint错误后,您的代码将直接运行,或者导致错误的原因显而易见。

Possibly when you drill into the jQuery libraries, you'll find that there are browser specific implementations for its classes and methods. 可能当您深入研究jQuery库时,您会发现针对其类和方法有特定于浏览器的实现。 I noticed this with its AjaxManager. 我注意到它的AjaxManager。

Perhaps you've made the mistake I talked about here: http://my.opera.com/hallvors/blog/show.dml/26650 somewhere in your code? 也许您犯了我在这里谈论的错误: http : //my.opera.com/hallvors/blog/show.dml/26650在您的代码中的某个地方?

Anyway, to really answer this question we'd need a link to a full page where the problem occurs. 无论如何,要真正回答这个问题,我们需要链接到发生问题的整页。

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

相关问题 简单的Javascript在FireFox中不起作用(但在所有其他浏览器中都有效) - Simple Javascript doesn't work in FireFox (but does in ALL other Browsers) 如何在Firefox以外的所有其他浏览器中实现此功能 - How to make this work in all other browsers other than Firefox 谷歌地图使用Fancybox完美地运行Firefox,但其他浏览器不起作用 - Google Map using Fancybox working Firefox perfectly but Other Browsers it Doesn't Work JPlayer在Firefox Android上不起作用,但在所有其他浏览器上 - JPlayer Does Not Work On Firefox Android But All Other Browsers Javascript firefox无法找到所有其他浏览器所做的值 - Javascript firefox can't find values all other browsers do 如何使该texarea javascript在Internet Explorer以外的其他浏览器中工作 - how to make this texarea javascript work in other browsers except Internet Explorer JavaScript按钮在Chrome和IE中完美启动,但在Firefox中不起作用 - JavaScript button fires perfectly in Chrome and IE but won't work in Firefox 如何获取适用于所有浏览器的javascript日期分割代码? - How to I get javascript date split code to work for all browsers? 脚本可以在除IE之外的所有其他浏览器中完美运行 - Script runs perfectly in all other browsers but IE waveurfer.js-使它在Firefox以外的浏览器中运行 - wavesurfer.js - getting it to work in browsers other than Firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM