Laran Evans
I develop software people love to use.
  • Home
  • About
  • Contact
  • Resume
  • Testimonials
Skip to content
  • Entrepreneurship
    • Leadership
    • Project Management
    • Team Development
    • Time Management
  • Puzzles
  • Software Architecture
    • Cloud Computing
  • Software Development
    • Algorithms
    • CSS
    • Java
    • Javascript
    • MySQL
    • PHP
    • Python
    • Ruby and Rails
    • System Administration
« Quaere – LINQ for Java
Symfony+Pear: Task “generate:project” is not defined. »

Degradable Javascript Links

By laran | Published: 2008/12/19

Here’s a useful technique for anyone doing Javascript/Ajax work.

It’s a common practice to invoke javascript functions from the onClick event of an element (a link, button or whatever). I often see them implemented a little wierdly though.

Solution #1 – The Usual

This is what I often see:

<a href="#" onClick="someFunction()">Click this. I know you want to.</a>

I don’t like this because it make the focus jump to the top of the page. That’s just wierd.

Solution #2 – For testing/debugging/development

One step better would be to do this:

<a href="#" onClick="someFunction(); return false;">Click This. I know you want to.</a>

This is better because by returning false, the focus won’t shift to the top of the screen.

This solution works poorly in the event that a user has javascript turned off.

Solution #3 – For production

In production, the best solution is this:

<a href="you_have_javascript_disabled.html" onClick="someFunction(); return false;">Click This. I know you want to.</a>

If the user has javascript enabled, your Javascript will run and the user will see what you intend them to see. If the user has Javascript disabled they will be taken to a page which tells them that since they have Javascript disabled, they’re probably not seeing what they should.

However, the one caveat here is that this third solution makes it difficult to debug the javascript inside the someFunction() function. If the function doesn’t behave as intended the developer will be taken to you_have_javascript_disabled.html and won’t see any Javascript errors.

So, for development, use solution #2. For production use solution #3.

This entry was posted in Javascript and tagged Javascript, tricks, Web Development. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
« Quaere – LINQ for Java
Symfony+Pear: Task “generate:project” is not defined. »

One Comment

  1. Yoh
    Posted 2009/09/16 at 2:35 PM | Permalink

    Degradable ajax, on theory, is that simple. However, to construct a proper back-end to handle the two events (with and without javascript enabled) requires alot of work. People focus much to Javascript side but they neglect the server side script. This guide should help you to understand the basic code setup for PHP that works together with degradable ajax http://www.silveryhat.com/delynie/f47/guide-how-to-make-ajax-url-link-becomes-seo-friendly-6828.html

Post a Comment

Click here to cancel reply.

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • About Me

    I've got a masters degree in computer science and over 10 years of experience building web-based systems using Java/J2EE, Ruby, Rails and PHP. I'm a strong believer in the effectiveness of Agile Methods. Read more »

  • Subscribe

  • Table of Contents
    • Solution #1 – The Usual
    • Solution #2 – For testing/debugging/development
    • Solution #3 – For production
  • Similar Posts
    • How to Write Gracefully Degrading Javascript onClick Event Handlers
    • 2008 Survey for People Who Make Websites
    • Excellent screencasts available on .NET MVC
    • Outsourcing Killed By Django And Ruby On Rails | Django Aware
    • Djammin' with Django
Hello world
  • From Around the Web

    • Chopper Girl @ The 2010 Brass Balls Long Road Party (Uploads from BRASS BALLS BOBBERS)
    • Web App Business Models: User Needs and What People Pay For (Box UK Blog)
    • Blog Writing Tips from the World’s Most Famous Authors (Blogsessive.com)
    • How to get exponential success on your blog (CatsWhoBlog.com)
    • The Four Stages of Growing a Blog (Daily Blog Tips)
    Shared Items
  • Recent Posts

    • 80 pages of Ruby on Rails Performance Optimization Tips
    • Ruby Garbage Collection In-Depth
    • Binary Logic Basics
    • Kuali in Nacubo Magazine
    • Ruby Blocks, Procs, Lambda
  • Older Posts By Month

    Let's Talk

    Ask a question below. You'll be prompted for your name and email after you click the "Ask" button.

Know more. Accomplish more. Succeed.