Archivo para May, 2008

Validate target attribute XHTML Strict or HTML 4.0 Strict

Saturday, 17 de May, 2008

For the standards lovers here we have a trick to simulate the “target” attribute behavior without break HTML 4.0 Strict o XHTML 1.0 Strict standards.

We will use the “rel”attribute with the ”external” value that validates correctly with the above standards. We will make an script that will look for all “<a>” elements with “rel” attribute equals to “external” and then will assign a new “onclick” event to it that open in a new window the URL stored on the “href” attribute .

<a href="http://www.equipo24.com" rel="external">Nice site developed with mootools</a>

Here we have the javascript for Mootools and another general solution without need of externals libraries.

Mootools

function fix_external_links() {
	$ES('a').each(function(el) {
		if (el.getProperty('rel') == 'external') {
			el.addEvent('click', function(e) {
				e = new Event(e);
				e.stop();	
				window.open(this.getProperty('href'));
			}.bind(el));
		}	
	});
}

window.addEvent('domready', function() { 
	fix_external_links();
});

Without external libraries

function fix_external_links() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("rel") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		} 
	}
}
window.onload = fix_external_links;

e24Squares - AJAX Pictures transition effect

Sunday, 4 de May, 2008

Intro

e24Squares - Galeria de fotose24Squares is an extension written for scriptaculous to make transitions between images with an original effect. This effect randomly generates squares that appear on different positions on the current image while the next image appears behind with a fade effect. The extension can be used as photo gallery, as slideshow, or as decorative animation in our web page.

Demos

Usage

Include Prototype 1.6.0.2 and Scriptaculous 1.8.1 in the HTML code header. Also include e24Squares after both:

<script type='text/javascript' src='js/prototype.js'></script>
<script type='text/javascript' src='js/scriptaculous.js?load=effects'></script>
<script type='text/javascript' src='js/e24squares.js'></script>

Next, write the necessary HTML code to define the gallery. We will need a container layer (”visor” on the example) and inside the container we will have an image for the first image in the gallery. Example below:

<!--e24Squares container -->
<div id="visor" >
	<img src="img/liberty.jpg" alt="imagen 1" />
</div>
<!-- End of e24Squares container -->

Finally, we write the necessary javascript code. As first param we will provide the container layer id (”visor”), and as second param an images filenames array and as the last one an option’s array.

oe24Squares = new e24Squares( 'visor', 
			    [ 'liberty.jpg', 'newyork2.jpg', 'mexico.jpg', 'newyork.jpg' ],
				{ 
					base: 'img/', 
					width: 640, 
					height: 300, 
					rows: 3, 
					cols: 5, 
					hideonfinish: true, 
					autostart: true, 
					duration: 1.0, 
					interval: 1.0 
				}
			);

Options

  • base: Relative path to the image directory. Default is the same directory as the web page
  • width: Width in pixels for the gallery visor.
  • height: Height in pixels for the gallery visor.
  • rows: Rows quantity in which the effect will divide the visor to form the random squares. Default value: 3
  • cols: Columns quantity in which the effect will divide the visor to form the random squares. Default value:5
  • duration: Decimal, representing the time in seconds that each transition between the current image and the next one will take. Default value: 1.0
  • interval: Decimal, representing the time in seconds that will take to begin the next image transition. Default value: 0.3
  • color: Represents the random squares color. Default value: #ffffff
  • autostart: Boolean, if set to “true” the animation starts automatically, if set to “false” then we will need to call start() method to initiate the animation or to call the transition() method to go to an specific image. Default value: “false”
  • hideonfinish: Boolean, if set to “true” then the visor will be hidden after showed the last photo, else the last photo will keep visible. Valor por defecto: false
  • callback: callback function that will be processed once the animation is complete. It is useful to concatenate animations.

Methods

  • start: No params needed. Initiates the slideshow if the effect object was created with the autostart option set to “false”.
  • transition((int)index, (function)pCallback): The first param is the picture index to show(the first is zero), the second param is a callback function that will be called once finished the transition.

oe24Squares.transition(2);

Known Issues

  • Ability to make the slideshow to play infinitely.

Download

e24Squares 0.2 beta (218 KB, including the demos)

Gracefully degrade with Prototype. The onDomReady event

Saturday, 3 de May, 2008

Following up on the previous post, we saw how important is to take care about our AJAX scripts to degrade gracefully. Today we will see how to do it in the practice using prototype.

We will take as example the e24Writer described on previous posts. The e24Writer gradually display a group of elements using a chained opacity transition, for that reason we need that those elements be hidden before the event run. For example if we going to animate the text “AJAX”, in the way that appear letter-by-letter. If we initially hide that text using the CSS display property setting it to none, then if due to any of the reasons described on the last post we don’t have javascript then the web won’t degrade gracefully because the text “AJAX” never will appear.

But if we initially leave the text visible then we will need to hide it before the page renders, to accomplish this we need an event that fires when we have total access to the DOM and before the page renders. That event is the “domReady”.

Some AJAX libraries like Mootools, YUI o ExtJs natively have the “domReady”event, Prototype don’t. But if we look on Google by the term “prototype domReady” then we will find some extensions like this one

Event.onDOMReady(function() {
  $('text').hide();
  oe24Writer = new e24Writer( 'text', { autostart: true, duration: 1.0, interval: 0.3 });
});

In this way the text can be read even when the javascript is disabled.