www.webdeveloper.com
Results 1 to 7 of 7

Thread: Help with a Javascript that works in almost all browser but IE

  1. #1
    Join Date
    Jan 2006
    Posts
    4

    Help with a Javascript that works in almost all browser but IE

    I have a javascript that works fine in almost every browsers with the exception of IE. the script is a function that allows you to click on a thumbnail and it loads the image by replacing an image. Does any one have an idea about how to get it to work in IE?

    http://angryteeth.net/gallery.shtml

    in the head:
    <!--

    function MM_findObj(n, d) { //v4.0
    var p,i,x;

    if(!d) d=document;

    if((p=n.indexOf("?"))>0&&parent.frames.length)
    {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }

    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n);
    return x;
    }

    function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;

    for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null)
    {
    document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
    }
    }

    -->

    in the body:

    <td><a href="javascript:;" onclick="MM_swapImage('largeimg','','img/.../2.jpg',1)"><img src="img/.../2thumb.jpg" name="defeat" border="0" height="50" width="50" alt=""></a></td>

    Thanks

  2. #2
    Join Date
    Dec 2005
    Location
    FL, USA
    Posts
    265
    Seems lite the code is made by a program - and those usually make sure that the script works everywhere. Are you sure you didn't change anything in the code?
    Am I a moderator?
    Well, what function are you going to use to find out?

  3. #3
    Join Date
    Aug 2005
    Location
    The Hill Ch' Hill
    Posts
    381

    Lightbulb Use a better version

    Here Take an early working of my code. Should be simple enough to implement.
    and works on all modern browsers... as I remember

    ----------------------------------------------------------------------
    put between <head>........</head> in HTML code.


    <script type="text/javascript">
    window.onload=function() {
    sourceImg=document.getElementById("source");

    imgs=document.getElementsByTagName("img");
    for (var i=0; i<imgs.length; i++){
    if (imgs[i].getAttribute("name") == "linked"){
    imgs[i].onclick=function() {
    sourceImg.src=this.src;
    }
    imgs[i].style.cursor="hand";
    }
    }
    }
    </script>



    change the "link images" to the thumbnails and "source image" to your initial image


    <body>
    <!-- link images -->
    <img name="linked" src="imgs/1.jpg"></img>
    <img name="linked" src="imgs/2.jpg"></img>
    <img name="linked" src="imgs/3.jpg"></img>
    <img name="linked" src="imgs/4.jpg"></img>

    <!-- source image -->
    <img id="source" src="sourceImg.jpg"></img>

    </body>

    When you have eliminated the impossible, whatever remains, however improbable, must be the truth !


    [Sir Arthur Conan Doyle]

  4. #4
    Join Date
    Dec 2005
    Location
    Queen Creek, AZ
    Posts
    1,564
    Use this set of functions instead:

    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;
    }
    Design first! Code later!

  5. #5
    Join Date
    Jan 2006
    Posts
    4
    Quote Originally Posted by CrazyMerlin
    Use this set of functions instead:

    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;
    }
    What do you use for your call line? I tried editing my <a href="javascript:;" onclick... line to match your code, but I must be missing something.

  6. #6
    Join Date
    Dec 2005
    Location
    Queen Creek, AZ
    Posts
    1,564
    simply change your: onclick="MM_swapImage('largeimg','','img/.../2.jpg',1)"

    to: onclick="FP_swapImg(1,0,/*id*/'largeimg',/*url*/'img/.../2.jpg');"
    Design first! Code later!

  7. #7
    Join Date
    Jan 2006
    Posts
    4
    Be patient with me I'm an artist learning web design so it may take me a min to understand.

    I copied the main js into a .js doc right from your post, as I did with my other code.

    and call it in the html

    <SCRIPT LANGUAGE="JavaScript" SRC="js-imgnavtest.js">
    </SCRIPT>

    I know the *ID* usualy means that I am supose to replace it with the ID I am using, but the script I was using before didn't use an ID. It just used the name=""

    I created a test page http://www.angryteeth.net/gallerytest.shtml

    <a href="javascript:;" onclick="FP_swapImg(1,0,/'limg',/'largeimg',/'img/.../left.jpg');"><img src="img/.../leftthumb.jpg" name="foot" border="0" height="50" width="50"></a>

    which is supose to swap the image in

    <td><div align="center"><img src="img/.../cedar.jpg" name="largeimg" id="limg"></div></td>

    I kind of guessed and added the id, id="limg", into this tag, but that doesn't seem to be what it wanted. Where should the id be placed?

    Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles