www.webdeveloper.com
Results 1 to 2 of 2

Thread: Classic javascript qestion

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Post Classic javascript qestion

    Whats the difference between:
    1) thisImage.onmouseover = rollOver
    2)thisImage.onmouseover = rollOver()

    I used option (2) and I got an Error saying:
    TypeError: this.outImage is undefined
    [Break On This Error]
    this.src = this.outImage.src;

    ---------------THE FULL SCRIPT IS BELOW the one that didn't work----------------

    window.onload = rolloverInit;

    function rolloverInit() {
    for (var i=0; i<document.images.length; i++) {
    if (document.images[i].parentNode.tagName == "A") {
    setupRollover(document.images[i]);
    }
    }
    }

    function setupRollover(thisImage) {
    thisImage.outImage = new Image();
    thisImage.outImage.src = thisImage.src;
    thisImage.onmouseout = rollOut();

    thisImage.overImage = new Image();
    thisImage.overImage.src = "images/" + thisImage.id + "_on.gif";
    thisImage.onmouseover = rollOver();
    }

    function rollOut() {
    this.src = this.outImage.src;
    }

    function rollOver() {
    this.src = this.overImage.src;
    }
    -----------THANKS AT ADVANCE---------------

  2. #2
    Join Date
    Nov 2010
    Posts
    1,090
    "this" is quite a powerful keyword in javascript, and you have to be careful which object it is referring to. used in a function it can refer to the function itself or the window object.

    I'ím not sure I follow your code, but I think it would be safer to do it something like this:

    Code:
    thisImage.onmouseover = rollOver(thisImage);
    
    function rollOver(pic) {
    pic.src = pic.overImage.src;
    }

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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