HTML Checked, Disabled and Selected attributes

Checked, Disabled and Selected are common HTML Boolean attributes. So what’s the correct way to specify these (and any other similar Boolean attributes) in your HTML markup? From the HTML 5 specs we get the following.

DO THIS

To add the attributes, you can do this:

<input type="checkbox" checked="checked">
<input disabled="disabled">
<option selected="selected">

Or this:

<input type="checkbox" checked="">
<input disabled="">
<option selected="">

And to not apply the attribute, just leave them off:

<input type="checkbox">
<input>
<option>

DON’T DO THIS

Leaving the attributes without a value is invalid:

<input type="checkbox" checked>
<input disabled>
<option selected>

Using true/false values is also invalid:

<input type="checkbox" checked="true">
<input disabled="false">
<option selected="true">
Posted in Web Development | Tagged , , , , , , | 3 Comments

Find the position of an element with JavaScript

Quirksmode provides a nice findPos() function to find the absolute position of an element on a page.

If you need to find the position of the element relative to the viewport then you will also need to know the page’s current scroll position, which I found over at Stack Overflow.

Bringing these couple together we get:

var util = {
 
	// findPos() by quirksmode.org
	// Finds the absolute position of an element on a page
	findPos: function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {	
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;	
			} while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	},
 
	// getPageScroll() by quirksmode.org
	// Finds the scroll position of a page
	getPageScroll: function() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;
		}
		return [xScroll,yScroll]
	},
 
	// Finds the position of an element relative to the viewport.
	findPosRelativeToViewport: function(obj) {
		var objPos = this.findPos(obj)
		var scroll = this.getPageScroll()
		return [ objPos[0]-scroll[0], objPos[1]-scroll[1] ]
	}
 
}

I needed to position a jQuery UI Dialog relative to another element on a page and found these functions worked well to get my dialog nicely positioned in IE6, IE7, IE8, Safari, Firefox and Chrome.

Posted in JavaScript | Tagged , , , , | 2 Comments

Google Guice AssistedInject example in Groovy

If you are using Google Guice to manage your object dependencies then you may find a situation where you have a constructor that has parameters required from Guice and some parameters from the calling code.

For example, suppose you are creating a Book which requires a BookService and a name when it is created:

class Book {
    Book(BookService bookService, String name) {
        ...
    }
}

In this case we want the BookService to be injected by Guice, but the name to be supplied by the calling code.

Guice provides something called an AssistedAnnotation that can help in this situation.

Read More »

Posted in Groovy | Tagged , , , | Leave a comment

Dependency injection with Groovy and Google Guice

I was looking for a lightweight dependency injection framework to use with Groovy and decided try out Google Guice. In this entry I am exploring version 2.0 without AOP.

This is just a quick overview of Guice and there is probably quite a bit more I haven’t discovered yet. I need to give credit to the guy over at dev.eek.be who has written up a great introductory Guice tutorial.

Read More »

Posted in Groovy | Tagged , , | 2 Comments

Parsing HTML with Groovy and HTMLCleaner

HTML found on the web can sometimes be invalid and difficult to parse. There are several HTML cleaning utilities that convert this invalid HTML to valid XML which is easier to work with. Two of these are Tag Soup and HTMLCleaner.

Tag Soup has a much nicer syntax when used with Groovy, but I decided to try HTMLCleaner because it is reported to have better results and is also used in the open source web scraping WebHarvest project.

Read More »

Posted in Groovy | Tagged , , , , , | 1 Comment