javascript-无法为以下对象加载源:http://localhost/js/m.js

javascript-无法为以下对象加载源:http://localhost/js/m.js,javascript,load,Javascript,Load,为什么哦为什么哦为什么。。。我不明白为什么我老是犯这个错误。我想我可能会哭 /*** common functions */ function GE(id) { return document.getElementById(id); } function changePage(newLoc) { nextPage = newLoc.options[newLoc.selectedIndex].value if (nextPage != "") { documen

为什么哦为什么哦为什么。。。我不明白为什么我老是犯这个错误。我想我可能会哭

/*** common functions  */
function GE(id) { return document.getElementById(id); }

function changePage(newLoc) {
   nextPage = newLoc.options[newLoc.selectedIndex].value

   if (nextPage != "")
   {
      document.location.href = nextPage
   }
 }

function isHorizO(){
  if (navigator.userAgent.indexOf('iPod')>-1)
    return (window.orientation == 90 || window.orientation==-90)? 1 : 0;
   else   return 1;

}

function ShowHideE(el, act){
    if (GE(el)) GE(el).style.display = act;
}

function KeepTop(){
    window.scrollTo(0, 1);
}

/* end of common function */

var f = window.onload; 
if (typeof f == 'function'){
    window.onload = function() {
        f();
        init();
    }
}else window.onload = init;

function init(){
    if (GE('frontpage')) init_FP();
    else {
        if (GE('image')) init_Image();
        setTimeout('window.scrollTo(0, 1)', 100); 
     }

    AddExtLink();
}

function AddExtLink(){
    var z = GE('extLink');
    if (z){
        z = z.getElementsByTagName('a');
        if (z.length>0){
            z = z[0];
            var e_name = z.innerHTML;
            var e_link = z.href;

            var newOption, oSe;
            if (GE('PSel')) oSe = new Array(GE('PSel'));
            else
                oSe = getObjectsByClassName('PSel', 'select')

            for(i=0; i<oSe.length; i++){
                newOption = new Option(e_name, e_link);
                oSe[i].options[oSe[i].options.length] = newOption;
            }
        }
    }
}

/* fp */
function FP_OrientChanged() {
    init_FP();
}

function init_FP() {
  //  GE('orientMsg').style.visibility = (!isHorizO())? 'visible' : 'hidden';
}

/* gallery */
function GAL_OrientChanged(link){
    if (!isHorizO()){
        ShowHideE('vertCover', 'block');
        GoG(link);
    }
    setTimeout('window.scrollTo(0, 1)', 500);
}
function init_Portfolio() {
 //   if (!isHorizO())
 //       ShowHideE('vertCover', 'block');
}
function ShowPortfolios(){
    if (isHorizO()) ShowHideE('vertCover', 'none');
}

var CurPos_G = 1
function MoveG(dir) {
    MoveItem('G',CurPos_G, dir);
}

/* image */
function init_Image(){
    // check for alone vertical images
    PlaceAloneVertImages();
}
function Img_OrtChanged(){
    //CompareOrientation(arImgOrt[CurPos_I]);
    //setTimeout('window.scrollTo(0, 1)', 500);
}

var CurPos_I = 1
function MoveI(dir) {
    CompareOrientation(arImgOrt[CurPos_I+dir]);
    MoveItem('I',CurPos_I, dir);
}

var arImgOrt = new Array(); // orientation: 1-horizontal, 0-vertical
var aModeName = new Array('Horizontal' , 'Vertical');
var arHs = new Array();

function getDims(obj, ind){
    var arT = new Array(2);

    arT[0] = obj.height; 
    arT[1] = obj.width;


     //arWs[ind-1] = arT;
    arHs[ind] = arT[0];

     //**** (arT[0] > arT[1]) = (vertical image=0)
     arImgOrt[ind] = (arT[0] > arT[1])? 0 : 1;

    // todor debug
    if(DebugMode) {
        //alert("["+obj.width+","+obj.height+"] mode="+((arT[0] > arT[1])? 'verical' : 'hoziontal'))
        writeLog("["+obj.width+","+obj.height+"] mode="+((arT[0] > arT[1])? 'verical' : 'hoziontal')+' src='+obj.src)
    }        
     if (arImgOrt[ind]) {
        GE('mi'+ind).className = 'mImageH';
     }

}
function CompareOrientation(imgOrt){
    var iPhoneOrt = aModeName[isHorizO()];
    GE('omode').innerHTML = iPhoneOrt;
    //alert(imgOrt == isHorizO())
    var sSH = (imgOrt == isHorizO())? 'none' : 'block';
    ShowHideE('vertCover', sSH); 

    var sL = imgOrt? 'H' : 'V';
    if (GE('navig')) GE('navig').className = 'navig'+ sL ;
    if (GE('mainimage')) GE('mainimage').className = 'mainimage'+sL;
    var sPfL = imgOrt? 'Port-<br>folios' : 'Portfolios' ;
    if (GE('PortLnk')) GE('PortLnk').innerHTML = sPfL;

}

function SetGetDim( iMInd){
    var dv = GE('IImg'+iMInd);

    if (dv)   {
        var arI = dv.getElementsByTagName('img');
        if (arI.length>0){
            var oImg =  arI[0];
            oImg.id = 'Img'+iMInd;
            oImg.className = 'imageStyle';
             //YAHOO.util.Event.removeListener('Img'+iMInd,'load');
             YAHOO.util.Event.on('Img'+iMInd, 'load', function(){GetDims(oImg,iMInd);}, true, true);
           //oImg.addEventListener('load',GetDims(oImg,iMInd),true);
        }
    }
}
var occ = new Array();
function PlaceAloneVertImages(){
    var iBLim, iELim;
    iBLim = 0;
    iELim = arImgOrt.length;
    occ[0] = true;
    //occ[iELim]=true;

    for (i=1; i<iELim; i++){
        if ( arImgOrt[i]){//horizontal image
            occ[i]=true;
            continue;
        }else { // current is vertical
            if (!occ[i-1]){//previous is free-alone. this happens only the first time width i=1
                occ[i] = true;
                continue;
            }else {
                if (i+1 == iELim){//this is the last image, it is alone and vertical
                    GE('mi'+i).className = 'mImageV_a'; //***** expand the image container
                }else {
                    if ( arImgOrt[i+1] ){
                        GE('mi'+i).className = 'mImageV_a';//*****expland image container
                        occ[i] = true;
                        occ[i+1] = true;
                        i++;
                        continue;
                    }else { // second vertical image
                        occ[i] = true;
                        occ[i+1] = true;
                        if (arHs[i]>arHs[i+1]) GE('mi'+(i+1)).style.height =  arHs[i]+'px';
                        i++;
                        continue;
                    }
                }
            }

        }


    }
    //arImgOrt

}

function AdjustWebSiteTitle(){

    //if (GE('wstitle')) if (GE('wstitle').offsetWidth > GE('wsholder').offsetWidth) {
    if (GE('wstitle')) if (GE('wstitle').offsetWidth > 325) {
        ShowHideE('dots1','block');
        ShowHideE('dots2','block');
    }
}

function getObjectsByClassName(className, eLTag, parent){
    var oParent;
    var arr = new Array();
    if (parent) oParent = GE(parent); else oParent=document;
    var elems = oParent.getElementsByTagName(eLTag);
    for(var i = 0; i < elems.length; i++)
    {
        var elem = elems[i];
        var cls = elem.className
        if(cls == className){
            arr[arr.length] = elem;
        }
    }
    return arr;
}



////////////////////////////////
///
// todor debug 
var DebugMode = (getQueryVariable("debug")=="1")
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  var sRet = ""
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
        sRet = pair[1];
    }
  }
  return sRet
  //alert('Query Variable ' + variable + ' not found');
} 
var oLogDiv=''
function writeLog(sMes){
    if(!oLogDiv) oLogDiv=document.getElementById('oLogDiv')
    if(!oLogDiv) {
        oLogDiv = document.createElement("div");
        oLogDiv.style.border="1px solid red"
        var o = document.getElementsByTagName("body")
        if(o.length>0) {
            o[0].appendChild(oLogDiv)
        }
    }        
    if(oLogDiv) {
        oLogDiv.innerHTML = sMes+"<br>"+oLogDiv.innerHTML
    }
}

如果你能提供更多的背景,这会有所帮助。例如,是

未能为以下对象加载源:

错误消息的文本?你在何时何地看到它

此外,该代码是否表示http://localhost/js/m.js? 看起来是这样,但很难说


在任何情况下,您展示的JavaScript都有相当多的语句缺少分号。可能还有其他语法错误。如果你自己找不到,你可能会找到一些有用的工具。

首先,Firebug是你的朋友,要习惯它。其次,如果您逐个粘贴每个函数和一些支持行,最终将得到以下结果

var DebugMode=getQueryVariabledebug==1 函数getQueryVariablevariable

您不能在定义getQueryVariable之前执行它,您可以为将来的引用创建一个句柄,但这是有区别的


代码中还有其他一些潜在问题,但是在getQueryVariable方法结束后放置var DebugMode行应该可以正常工作。

确保标记中的type属性是text/javascript而不是script/javascript。

我知道这个问题提出已经一年多了,但我今天就面对了这个问题。我有一个

<script type="text/javascript" src="/test/test-script.js"/>
我在Firebug中得到了“加载源代码失败”错误。甚至chrome也没有加载这个脚本。然后我将上面的行修改为

<script type="text/javascript" src="/test/test-script.js"></script>

它开始在Firefox和chrome中工作。在这里记录这些,希望这能帮助别人。顺便说一句,我不知道为什么后一个在前一个不起作用的地方起作用。

思考过程:有些东西不起作用,令人沮丧,去stackoverflow,创建一个单词问为什么,转储一堆没有解释的代码,想知道为什么所有的否决票。如果你想帮助发布一个有相关细节的连贯性问题。为什么。。。人们会问这样的问题吗?这是一个问题;分号在Javascript中是不必要的,除了在同一行上分隔语句外。@unclo但是,鉴于缺少分号可能会对多行语句产生意外后果,仍然建议使用分号。罪名成立。我太习惯于看到它们了,即使是在JavaScript代码中,我忘记了它们通常可以省略。