www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: Javascript error: Obj has no properties

  1. #1
    Join Date
    Aug 2005
    Posts
    14

    Arrow Javascript error: Obj has no properties

    I have a number of show/hide images on my site which allows the user to click on various thumbnails to open up the full image using layer based styles.

    However, my Javascript seems to have developed an error saying that the object has no properties and then takes me to the following line of code:

    obj.visibility='hidden';

    This code was written for me so that i could use the script dynamically with PHP an alter the number of div layers it needed to generate.

    The full(?) javascript for this function is:

    function showImg(myShow)
    {
    var loopNum=99;
    for (x=1; x<=(loopNum); x++) //1 based loop
    {
    myImg = "image" + x;
    if ((obj=MM_findObj(myImg))!=null)
    {
    if (obj.style){obj=obj.style;}
    }
    if (x == myShow)
    {//show it!
    obj.visibility='visible';
    }else{//hide it!
    obj.visibility='hidden';
    }
    }
    }


    I would be enorumously grateful if someone could help me here to pinpoint the error. Let me know if I have missed out any detail/code

    Lee

  2. #2
    Join Date
    Aug 2005
    Posts
    35

    Can we have more data?

    Lee

    can you give us the code for function MM_findObj(myImg) as it is this
    that set the value of obj and it is a bug in there that could well be the
    cause of your problems.

    Steve

  3. #3
    Join Date
    Mar 2005
    Posts
    767
    You need to nest the 'if (x == myShow)' block inside the 'if ((obj = ...)' block.

  4. #4
    Join Date
    Aug 2005
    Posts
    14

    the MM_findObj(myImg) code

    Quote Originally Posted by DarkRedSpiral
    Lee

    can you give us the code for function MM_findObj(myImg) as it is this
    that set the value of obj and it is a bug in there that could well be the
    cause of your problems.

    Steve
    I think this is what you are looking for:

    function MM_findObj(n, d) { //v4.01
    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 && d.getElementById) x=d.getElementById(n); return x;
    }


    I have included the following code which is still in my file but I think it is redundant now:

    function MM_showHideLayers() { //v6.0
    var i,p,v,obj,args=MM_showHideLayers.arguments;
    for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible'v=='hide')?'hidden':v; }
    obj.visibility=v; }
    }


    Thanks Steve!

  5. #5
    Join Date
    Aug 2005
    Posts
    14
    Quote Originally Posted by Orc Scorcher
    You need to nest the 'if (x == myShow)' block inside the 'if ((obj = ...)' block.
    Thanks, but could you explain it in another way please?

  6. #6
    Join Date
    Mar 2005
    Posts
    767
    That's what you have now, more readable:
    Code:
    function showImg(myShow)
    {
        var loopNum=99; 
        for (x=1; x<=(loopNum); x++) //1 based loop
        {
            myImg = "image" + x;
            if ((obj=MM_findObj(myImg))!=null)
            { 
                if (obj.style){obj=obj.style;}
            }
            if (x == myShow)
            {//show it!
                obj.visibility='visible';
            }else{//hide it!
                obj.visibility='hidden';
            }
        }
    }
    It should be nested like this:
    Code:
    function showImg(myShow)
    {
        var loopNum=99; 
        for (x=1; x<=(loopNum); x++) //1 based loop
        {
            myImg = "image" + x;
            if ((obj=MM_findObj(myImg))!=null)
            { 
                if (obj.style){obj=obj.style;}
                
                if (x == myShow)
                {//show it!
                    obj.visibility='visible';
                }else{//hide it!
                    obj.visibility='hidden';
                }
            }
        }
    }

  7. #7
    Join Date
    Aug 2005
    Posts
    14
    Thanks for that modification, It works now that I have modified the curly braces but in doing so I have now created another issue:

    With the error, the onmousedown command (attached to the thumbnail) INSTANTLY displayed the main image. With this modification, it takes over a second for the image to appear. What's up with that?

    Just curious: Both ie6 and Firefox worked with the javascript error (though ie6 says 'with errors' at the bottom). Does it really matter if javascript has errors just so long as it works?

    Cheers!

  8. #8
    Join Date
    Mar 2005
    Posts
    767
    Well, I'd say having that script error warning icon on your page looks quite amateurish. I don't know how many images you actually have on your page, lets say 10: In that case the fixed code has to do 10 times as much work and the MM_findObj function is a slow beast.

    If you are just interested in finding IMG tags, try replacing 'obj=MM_findObj(myImg)' with 'obj=document.images[myImg]', that should give a nice performance boost.

  9. #9
    Join Date
    Aug 2005
    Posts
    14

    Arrow

    Quote Originally Posted by Orc Scorcher
    Well, I'd say having that script error warning icon on your page looks quite amateurish. I don't know how many images you actually have on your page, lets say 10: In that case the fixed code has to do 10 times as much work and the MM_findObj function is a slow beast.

    If you are just interested in finding IMG tags, try replacing 'obj=MM_findObj(myImg)' with 'obj=document.images[myImg]', that should give a nice performance boost.
    Firstly I want to thank you for your support in helping me repair my galleries with this javascript issue. I had no idea that the function was slow. All this is beyond my javascript knowledge (i have just graduated!) so I am struggling to understand. If you don't mind I would like to ask some specific questions based on your reply:
    1. What do you mean to say my script error warning icon looks amatuerish?
    2. Are you saying just to replace 'obj=MM_findObj(myImg)' with 'obj=document.images[myImg]' - this i did but now it does not work at all!
    3. Does it matter if the page has javascript errors? Is just that before it worked - and it worked fast! (I have upto 60 thumbs and the same in full size images on a page.)


    Or maybe there is a better function for doing what I am trying to do?

    Thanks again!

  10. #10
    Join Date
    Mar 2005
    Posts
    767
    Quote Originally Posted by tintin2002
    [*]What do you mean to say my script error warning icon looks amatuerish?
    Well, when a script produces an error, IE show a yellow exclamation mark in the bottom left corner and the message "Error on page" in the status bar, other browsers report the error in the JS console. For the more computer-savy visitors this give the impression the page author has done a poor job; others could be confused: "Have I done something wrong?" "Must I correct this error? How?" "Is the page functional?"
    Quote Originally Posted by tintin2002
    [*]Are you saying just to replace 'obj=MM_findObj(myImg)' with 'obj=document.images[myImg]' - this i did but now it does not work at all!
    I wasn't very optimistic it would work. Since it don't know what your HTML looks like, I could only make a wild guess.
    Quote Originally Posted by tintin2002
    [*]Does it matter if the page has javascript errors? Is just that before it worked - and it worked fast! (I have upto 60 thumbs and the same in full size images on a page.)
    See 1. There's certainly a way to this implement fast and bug-free, but not without seeing the complete page. The least thing you can do is hide this error from your users by including
    Code:
    window.onerror = function () { return true }
    at the beginning of your script.

  11. #11
    Join Date
    Aug 2005
    Posts
    14

    Javascript Error and Conflict with displaying and hiding images

    Thanks for the feedback. I will implement the code to hide any javascript errors but I guess prevention is better than cure!

    Listen, I don't know what is in this for you, other than the fact you seem to be an extremely nice guy(?), but I would love you to look at the complete page if that is appropriate and you are willing.

    I have missed out one other issue with the page that I would truly love to solve as I use this code on so many pages; It is used for viewing images - and the user can either do this by clicking on the thumbnails displayed on the left (the javascript then displays the main image as already discussed). The other method is a secondary javascript action that displays the images slideshow style with next and previous buttons. The issue I have is that there is a display conflict with the code when the user switches between the two methods to display the image. Simply put, the user has to wait for the slide show to 'catch up' with the order (this is especially noticable when I have both portrait and landscape images). Does this make sense?

    If you can look I would happily post the code or post a link (i don't know what the correct protocol is for this) and if you were ever in Seattle I'd buy you a beer!

    Cheers!

  12. #12
    Join Date
    Aug 2005
    Posts
    3

    Cool

    Everyone is willing to help with any JavaScript problem. Just post any (or all) of your code that you want help with here and we will all enjoy flexing our mental muscles at each other over your problem.

    The reason the Orc's speed-up didn't work is that myImg is the *name* of the image not the number. I'll try to explain this.

    document.images[*num*] will access the images in the order that they appear in the document (starting from 0). So, document.images[0] will access the first image, chronologically, in the document. However it has to be a number, not a name.

    So the proper fix should be to put this in the place of "if ((obj=MM_findObj(myImg))!=null)":

    Code:
    if(obj=document.images[x-1]!=null)
    This is assuming that your images are numbered in the order they appear, and that they start from 1.

    give it a try, let us know how you get on, and post anything else you want to.

    Robin.

    * Edit: By the way, if this works you can delete the line "myImg = "image" + x;" which is now redundant.

    * Edit 2: Actually, this won't work if you have *any* other images in the document, because it'll find them as well, so the image you're trying to reference won't have the correct reference number. I'll look into the problem and post again. sorry. (Although it was orc's idea not mine )
    Last edited by RahinRedBreast; 08-22-2005 at 03:29 PM.

  13. #13
    Join Date
    Aug 2005
    Posts
    3

    Post

    I have been taking a lot of things for granted which should really be questioned.

    I may be being stupid but I don't see why you need all the looping 98 times business. You can, as far as I can see, achieve exactly the same thing with this simple function (which won't take any time at all):

    Code:
    function showImg(myShow) {
        myImg = "image" + myShow;
        MM_findObj(myImg).style.visibility="visible";
    }
    Assuming that when you call 'showImg(1)' the image you are hoping to reveal will have the name tag "image1":

    HTML Code:
    <img src="..." name="image1" visibility="hidden" />
    If this is the case then this will work fine on it's own.
    Admittedly if there isn't an image with the corresponding name tag then it will throw the javascript error "MM_findObj(myImg) has no properties", but I assume that the method will only be called from static documents and therefore if the image doesn't exist you probably want it to throw an error to alert you anyway.

    Let me know if there's some reason why this won't work. And please do post those other problems of yours. I am in the process of learning JavaScript myself and I want some practice.

    Robin.
    Last edited by RahinRedBreast; 08-22-2005 at 03:26 PM.

  14. #14
    Join Date
    Aug 2005
    Posts
    1
    how can jou cheat at games at javascrit

  15. #15
    Join Date
    Aug 2005
    Posts
    3
    Montanatony, unless it's extremely badly written u can't. Now why don't you just play the games properly.

    Okay back to the main problem. Now, as I stated above, my little script doesn't really work. However I have a couple of options which will run faster than the current solution. I'll put the current solution here for reference:
    Code:
    function showImg(myShow)
    {
        var loopNum=99; 
        for (x=1; x<=(loopNum); x++) //1 based loop
        {
            myImg = "image" + x;
            if ((obj=MM_findObj(myImg))!=null)
            { 
                if (obj.style){obj=obj.style;}
                
                if (x == myShow)
                {//show it!
                    obj.visibility='visible';
                }else{//hide it!
                    obj.visibility='hidden';
                }
            }
        }
    }
    Okay, numero uno. I believe that this could run significantly faster just by moving the curly brace at the end of "if (obj.style){obj=obj.style;}" to encompass the whole of that block. Here is my (differently formatted) version of this solution:
    Code:
    function showImg(myShow) {
        var loopNum=99; 
        for (x=1; x<=(loopNum); x++) { //loop 99 times
            myImg = "image" + x; //create image name
            if (obj=MM_findObj(myImg)) { 
                if (obj.style){
                    obj=obj.style;
                    if (x == myShow) obj.visibility='visible';
                    else obj.visibility='hidden';
                }
            }
        }
    }
    My other idea was to go with the document.images idea but modify it so it would have the same functionality. This should be faster because it doesn't have to test every object in the document, just the images. Here it is:
    Code:
    function showImg(myShow) {
        myImg = "image" + myShow;
        var currentImg = document.images;
        var i = 0;
        while(currentImg[i]) {
            if(currentImg[i].name==myImg) currentImg[i].style.visibility="visible";
            else currentImg[i].style.visibility="hidden";
            i++;
        }
    }
    I believe this second solution should be faster, but I don't have a html page with 60 or so images to test it on, and I can't be bothered to make one. so try them out. It's easy, just copy the script exactly as it is from here and overwrite the current "showImg()" function in your javascript (making sure you've backed up first) and then post here to let me know if they work, and which is better,

    Robin.
    Last edited by RahinRedBreast; 08-22-2005 at 07:04 PM.

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