Archive for August, 2009

How to add a webpage fade-in effect using JQuery

Friday, August 28th, 2009

THE PROBLEM: HTML pages that have numerous and complex nested <div> tags and CSS can sometimes have a jumbled “assembling” appearance that happens as the page is loading and the various elements are being rendered by the browser. The appearance of this process can look quite unrefined and detract from professional appearance of the site.

A SOLUTION: JQuery includes a function to fade-in a hidden tag. If the content of the page is initially hidden using CSS, it then can be faded-in using JQuery once the whole page has been processed and is ready for display.

Step 1

Inside the page’s <body> tag, enclose the content in a <div> tag with a unique CSS id (see this post for the difference between CSS ids and classes).

<body>
 
<div id="content-wrapper">
 
The page content goes here!
 
</div><!-- content-wrapper -->
 
</body>

Step 2

Inside the page’s <head> tag, import the JQuery library (download it here) and create a fade-in effect on the <div> tag surrounding the page’s content. Key to this solution is that the effect gets placed inside JQuery’s $(document).ready() function, ensuring the fade begins after the document’s DOM has been completely parsed.

<script type="text/javascript" src="jquery.min.js"></script>
 
<script type="text/javascript">
$(document).ready(function() 
{
		// fade in content.
		$( '#content-wrapper' ).fadeIn("slow");		
});
</script>

Step 3

At this point the fade-in effect will not occur, because the content is not hidden to begin with. To correct this add some CSS styling underneath the JavaScript above to hide the page content initially.

<style type="text/css">
 
#content-wrapper
{
	display:none;	
}
 
</style>

Step 4

Now the fade-in effect will work, however, a problem exists. If someone visits the page and has JavaScript turned off in their browser, the CSS to hide the content will be applied, but the JQuery to fade it back into visibility will not run, leaving the page effectively blank. This is a serious accessibility issue. To correct this, instead of hardcoding the <div> tag that is wrapping the content, write the tag using JavaScript. That way if JavaScript is turned off, the tag will not be rendered and won’t hide its containing content. Change what was written in Step 1 to the following:

<body>
 
<script type="text/javascript">
<!--//--><![CDATA[//><!--
document.write( '<div id="content-wrapper">' );
//--><!]]>
</script>
 
The page content goes here!
 
<script type="text/javascript">
<!--//--><![CDATA[//><!--
document.write( '</div><!-- content-wrapper -->' );
//--><!]]>
</script>
 
</body>

Putting it all together you end up with this (remember to include the JQuery library in the same folder as this HTML page):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Fade-in effect example</title>
 
<script type="text/javascript" src="jquery.min.js"></script>
 
<script type="text/javascript">
$(document).ready(function() 
{
		// fade in content.
		$( '#content-wrapper' ).fadeIn("slow");		
});
</script>
 
<style type="text/css">
 
#content-wrapper
{
	display:none;	
}
 
</style>
 
</head>
 
<body>
 
<script type="text/javascript">
<!--//--><![CDATA[//><!--
document.write( '<div id="content-wrapper">' );
//--><!]]>
</script>
 
The page content goes here!
 
<script type="text/javascript">
<!--//--><![CDATA[//><!--
document.write( '</div><!-- content-wrapper -->' );
//--><!]]>
</script>
 
</body>
</html>

The dilemma of Twitter

Wednesday, August 19th, 2009

Twitter. Love it, perhaps hate it, or don’t get it. One thing is clear, Twitter has experienced profound media exposure, perhaps surprisingly out of proportion with the service it actually provides. No, there are no tools to help you sell things, no you can’t create galleries of your favorite pictures, no there isn’t a lengthy profile to fill out, no you can’t embed video. Twitter is simply 140 characters of whatever you want. Twitter is the bottled water of the Internet, in foresight dubiously useful, in hindsight surprisingly popular. With all this exposure has Twitter reached maturity and left the realm of novelty, or is it still riding a honeymoon through the limelight?

The tinkerers new toy

Twitter’s simple elegance has led many to explore creative ways of utilizing their 140 characters. Take Mario Klingemann’s (Quasimondo) Mona Lisa stored in a single tweet, whose rendering certainly needs no reference image to be recognizable. Or Grant Skinner’s Tweetcoding contest, a challenge to create something in ActionScript 3 within 140 characters. The small message footprint is seen as a challenge to mold and work within instead of an obstacle to overcome. It has the appeal of tinkering with an old toy, whose functionality may seem limited, but that very limitation makes it appealing to extend with modern tools, to see what is possible with what is available.

Taking a stroll down The Museum of Modern Betas, a website devoted to showcasing emerging beta websites, more often than not a new service showcased is one that aims to extend the functionality of Twitter.

Why extend the functionality of a site that could have more built into it to begin with, or spend time beating the most out of 140 characters when there are options available to disseminate texts of much larger length? The answer may lie in the special place Twitter holds within the eyes of its devotees, as well as the surprising versatility its simplicity allows.

A failure made for a t-shirt

Take for instance, the appearance of the Fail Whale—which, let us remember, denotes a failure of the system to function. On a whole, the flying whale’s appearance is met with mild irritation, but eventual forgiving acceptance. As if we’ve just missed the bus, but it’s a beautiful day out so why not wait a little longer. The whale appears, but it will pass. What other service enjoys such forgiveness on failing?
Perhaps it is because Twitter has no aura of presumptuousness or expectation. Twitter didn’t tell you to use its service, or how to use it. Twitter didn’t try to sell you something. Twitter didn’t promise something big. Twitter just gives one simple service with clearly defined boundaries, the rest is up to you.

Prior to being usurped by Facebook in popular favor and acquiring a reputation of ugliness, MySpace followed a similar road to success. It gave you the tools to create a webpage, link it to your friends (and strangers), and decorate it any way you saw fit. Its tools enabled something without being overbearing. Twitter’s format enables a particular kind of messaging to take place, and even more subtly, Twitter enhances its service within its limitations. The short message format forces conciseness, producing messages that are composed of the bare essentials. This format offers an effective way to convey information quickly in a way that is analogous to the “sound bites” of broadcast media.

Riding the honeymoon

Fundamentally, no matter the length of the message, Twitter is just like other media, it is nothing without the content it contains. The millions and millions of tweets. From the mundane and trivial, to the very present and important. They are the daily messages from people across the globe, some of which have been the first to report breaking news (for example, US Airways Flight 1549’s landing in the Hudson River). Twitter has been a face for citizen journalism and along with sites like Facebook have brought the term “social media” into mainstream use. Lest we forget, however, the Web has always been social media from its roots. The World Wide Web established a protocol for linking documents together through hyperlinks, the very type of links you click on webpages today. These were documents created by people, with links added by people elsewhere to make sharing information easier. The effort of masses of people created a vast network of documents, linked in (hopefully) relevant ways. This group effort is the basis of what makes the Internet so wonderful. Incrementally a vast library of information has accumulated, accessible from around the world. Google’s mission of “organizing the world’s information” is as noble as it is important. Web development should, as it always has, concern itself with organizing what information is on the Internet.

It is no surprise then that there are so many services appearing that attempt to utilize and extend the information on Twitter. The challenge is how to extract the useful information from the trivial and meaningless tweets. However, as the saying goes “one man’s trash is another man’s treasure,” and what is “useful information” is not always clear. And herein lies the dilemma of Twitter. Its flexibility within its confines is powerful, yet limiting. It is a versatile tool for the goals of many audiences, but without a clear way to target any particular one. All users are sending tweets into the pool without any inherent way of organizing them, since tweets lack any user generated metadata outside of the messages themselves. Yes, there are the existing hashtags, however, these rely on formatting conventions, and as with any system without standards they may become disparate in implementation, reducing their efficacy. Twitter’s advanced search is certainly useful, but it is a way to mine information, not consume relevant information in a passive way, the latter of which would be more appropriate to Twitter’s format.

Additionally, the character limit of messages, while keeping things concise, does not work with long URLs. Encouraging the spread of URL shortening services does nothing for the reliability of the information Twitter holds (see my other post: Why URL shortening is bad), unless Twitter itself acquires one of these services. If Twitter is more than a novelty or tinkerers toy, whose database of moments is to be taken as an important resource to be preserved on the Internet, tackling these issues may become tied with the continued sustainability of Twitter. At very least Twitter will need to utilize its own information in a way that offers a financial gain, since as of yet Twitter, the actual company, has yet to have any revenue from their namesake. For the time being, if the acceptance of the Fail Whale is any indication, Twitter may still be riding its way through the limelight.

Why URL shortening as we know it is bad

Monday, August 17th, 2009

There was a time when computers on the Internet were accessed via a numerical address. Early on, even before TCP/IP, the idea to map these sets of numbers to human-readable addresses seemed like a good one. The precursor of what is today the Domain Name System (DNS) was born. Fast forward to today and the Web is filled with millions of domain names. Familiar ones such as http://google.com are typed daily instead of http://74.125.45.100. As more and more domains were added and more and more server-side technologies emerged, URLs of webpage addresses became long and cumbersome. A need for a shortened URL was needed.

Early in the 2000s a new website, tinyurl.com, became the first notable site to provide such a service. Using TinyURL any website address could be given an alias that redirected to the actual address, but the alias often contained many fewer characters than the actual address. This was seemingly ideal for pasting to chat windows, sending in email (where email clients would sometimes not parse web addresses that contained long query strings with question marks and ampersands correctly), etc.

Then in 2006, Twitter appeared on the scene, and with its character limited message format of 140 characters, the need for a URL shortening method was all the more in demand. With Twitter’s meteoric rise to prominence, URL shorteners were given a large helping hand to mainstream visibility. In the time since, dozens have sprung up. But without a revenue stream to sustain them these services often folded just as quickly. And herein lies the problem. The Internet is built upon links of information that are relevant and accessible. For the most part these links are created by humans, and once created add to the organization and usefulness of the Internet as an information resource. Through Twitter and elsewhere URL shorteners are creating a vast catalog of links that are dependent on one website service to exist. If the service disappears, the alias no longer redirects, and the link is dead. Unlike most dead links it also doesn’t offer a hint as to what the original resource was, because the address has been obscured beyond recognition (although, at least one service, twi.bz, attempts to retain some hint of the original address). This contradicts the very idea of the Internet itself, whose beginnings began in de-centralization.

Little more than a year and a half ago mashable.com had an article that listed “90+ URL Shortening Services.” Browsing through this list it can be discovered that many are already defunct. With so many services to choose from who’s to know which may be solid, sustainable enterprises and which may be a mere pet project. Here today, gone tomorrow. Smalltime services may not have the resources, financial or otherwise to sustain themselves, yet their aliases persist on the Internet. Take cli.gs, who lost many aliases earlier this year due to being hacked. While this could happen to any website, it emphasizes the centralized vulnerability of these services. A similar problem can occur in cloud computing, as was illustrated by the demise of Ma.gnolia due to a hardware failure.

There are so many of these services because they do offer something valuable that is not possible with the current Internet protocols. Take tr.im, who announced last week that they would be shutting their doors at the end of the year. The public reaction that followed made them change their mind. This certainly illustrates the demand for these services, and is good that for the time being, tr.im aliases will remain alive, but it is a band aid on a problem that needs a grander solution than one popular website can provide.

Projects such as shortlink and short_url aim to tackle this problem by removing the third-party aspect of URL shortening and creating a specification for creating shortened URLs on the site that hosts a particular URL. These projects are not yet polished, but it is a start.

Taken as inspiration from this article about a service called Vanish, one interesting use for URL shortening that uses aliases may be in links that are intentionally made to “expire” after a certain period of time. Since a particular URL shortening service controls where an alias redirects to, they also can control for how long as well. However, if such a service was used more generally it would lead to the same issues of dead links (linkrot) as above.

Creating a date countdown timer in ActionScript 3 / Flash

Monday, August 3rd, 2009

Creating a countdown timer in ActionScript is quite easy. The essentials of it are:

  1. Specify a target date to countdown to using the Date class.
  2. Create a Timer instance to check the countdown time each second or so.
  3. At each tick of the timer calculate the number of milliseconds between now and the target date.
  4. Determine how many days, hours, minutes, seconds, etc. that number of milliseconds is equal to.
  5. Update the graphics to reflect the amount of time left.

The following is an example of a countdown timer, with a link to the source code below. This utilizes a reusable class file for the countdown timer. All code specific to this particular implementation can be found in com/anselmbradford/Main.as.

This movie requires Flash Player 9

Download Source