SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Simply Javascript: Feedback please?

    Hi,

    I've just bought the Simply Javascript book, and I'm absolutely thrilled with it - and with writing object oriented javascript

    I've tried to replicate the "clickable area" function used on http://www.vivabit.com/atmedia2007/ by using the principles of this book, and I would be most grateful if I could possibly get some feedback on my very short script. Does it seem sensible?

    The idea is to grab the first url in every div element that has an "area" classname, and make the whole div element clickable. There's also an "over" class which changes the appearence of the element.

    Thanks,

    Pøbel

    Result: http://nystad.net/pobel/

    Code JavaScript:
    var clickArea = {
    	init: function() {
        var area = Core.getElementsByClass("area");
     
    	for (i = 0; i < area.length; i++) {
    			Core.addEventListener(area[i], "click", clickArea.mClick);
    			Core.addEventListener(area[i], "mouseover", clickArea.mOver);	
    			Core.addEventListener(area[i], "mouseout", clickArea.mOut);			
    		}
      	},
     
    	mClick: function() {
    		var link = this.getElementsByTagName('a')[0].href;
    		location.href = link;
    	},
     
    	mOver: function() {
    		Core.addClass(this, "over");
    	},
     
    	mOut: function() {
    		if (Core.hasClass(this, "over")) {
    			Core.removeClass(this, "over");
    		}
    	}
    };
     
    Core.start(clickArea);

  2. #2
    SitePoint Zealot GiorgosK's Avatar
    Join Date
    Jun 2007
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you want feedback on ?

    your javascript seems to be working ...

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd like to know if the code seems reasonable, or if it can be optimized/simplified but still object oriented

  4. #4
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Looks very reasonable as-is. The only change I'd suggest is to cancel the default action associated with the click event, since you are responding to the click yourself.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  5. #5
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just had to know if I was on the right track - thanks a lot


Bookmarks

Posting Permissions

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