Javascript in ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • soulfly73
    New Member
    • Sep 2007
    • 7

    Javascript in ASP.NET

    Hello All,

    I have implemented an Javascript inside an ASP.net page which raises an runtime error. "Object exspected". From wich I don't know what's the problem.
    The Javascript is an external script imported into the project. I have put the following code in my codebehind file

    Code:
            Page.RegisterStartupScript("PicitureSwitch", "<script language=javascript src='Images.js'>")
    It stops at the following line:
    Code:
    <body bgcolor="#FFFFFF" onload="FP_preloadImgs(/*url*/'images/Product/19154816.jpg', /*url*/'images/Product/19154816.jpg')">
    where the FP_preloadimgs is an function in the script.

    This is the actual JavaScript:
    Code:
    <!--
    	function FP_swapImg() {//v1.0
    	var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=2; n<args.length;
     	n+=2) { elm=FP_getObjectByID(args[n]); if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;
     	elm.$src=elm.src; elm.src=args[n+1]; } }
    }
    
    function FP_preloadImgs() {//v1.0
     var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
     for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }
    }
    
    function FP_getObjectByID(id,o) {//v1.0
     var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
     else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
     if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
     for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }
     f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
     for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }
     return null;
    }
    // -->
    Does anyone know what I am doing wrong?
    Thank's in advance!!!
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well I can tell you that this is wrong:
    Page.RegisterSt artupScript("Pi citureSwitch", "<script language=javasc ript src='Images.js' >")

    You could just stick this line in your aspx page:
    <script language=javasc ript src='Images.js' />

    That will include the javascript functions contained in the Images.js file. You can then call them out wherever you would like.

    I would bet if you looked at your source code you would see some really funky nested <script lines

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Try using the Page.ClientScri pt.RegisterClie ntScriptInclude () to register your script include instead.

      If your ASPX page is Ajax enabled (has a ScriptManager), you should register your script with the ScriptManager instead of the Page or Page.ClientScri pt.

      -Frinny

      Comment

      Working...