我使用FireFox作爲我的主要瀏覽器,特別是在測試我的網站時,Avoru。但是,當檢查我的代碼在其他主流瀏覽器(Google Chrome,Opera和Safari)中是否正常工作時,我發現我的自定義JavaScript似乎都不起作用。儘管頁面源代碼中的函數和代碼都很明確,但使用typeof爲我的所有函數返回了「未定義」值。JavaScript如何在FireFox中完美工作,但在其他瀏覽器中完全不能工作?
這個問題的根源是什麼?如果它有什麼區別,我使用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);
});
給我們一些代碼與... – 2010-08-01 06:13:25