Javascript

Filling a select drop-down with ajax data using jQuery

In user interfaces, it’s sometimes the case, that you need to have dropdowns with content dynamically loaded from a database, and sure you can query the database and place the content in the HTML-source as regular markup, but you can also write a few lines of jQuery powered javascript, and provide the data from an ajax-source.

Here’s how to do it.

First make the drop-down and as the single option make a “loading” message - just to let the user know something is happening.

Browser handling of broken includes in an Ajax world?

In this mordern web 2.0 age, javascript includes are used more and more - and in some cases solutions often depend on included content from 3rd parties. While it is nice these 3rd parties may provide us with data (such as the latest tweets from twitter), it would be quite nice, if issues on their server didn’t break the fancy page we’re building.

So the question is this: If you build a page and include javascript from foreign scripts, will error messages on their part break your site? We’ve made the assumption, that the Web2.0 page will degrade gracefully, so that the content/functionality of the javascript-includes is just extra frosting, not needed to use the page

Building image tags with jquery

Like tables, jQuery also allows you to play around with all other tags. In a recent project, we wanted to change a fixed set of labels/texts into icons instead. For this challenge jQuery was used to build the new image tag, and for good measure, here is a small example of how you can build an image tag - including alt-text and click-event.

The basic code for building and inserting an image into the DOM looks like this:

jQuery and tables

I’ve been playing with jQuery the recent weeks, and while it is reasonable simple to get started with and the jQuery documentation is pretty good, I thought I’d share a few snippets of jquery examples/code, which showcases a few simple practical uses for jquery.

First up - tables and jquery.

With jQuery you can add content to a HTML table. Here are to simple examples on how to add content into a table.

Simple slideshow updated

Quite awhile ago I posted a little piece of javascript, which could be used to a (very) simple webpage slide show. Someone requested a version a little more advanced for use in a kiosk mode, and sure why not - updating the script with the option to set different times on the webpages displayed was a minor change, and it’s been done now.

If you’d like an updated version of a basic webpage slideshow with an option to specify the time each page is displayed, go ahead get it from the lab.

Simple Webpage Slideshow

At work we produce a few websites and have a few “web dashboards”. Wouldn’t it be nice, if public screens around the office could play a little loop mixing the websites and the dashboards together in a slideshow loop?

After an hour of javascript debugging, a nice little generic webpage slideshow was put together, and if you have a similar need a copy is now available in the lab. It’s simple, it should work in most browsers, and it probably has the least features of any slideshow out there.

Changing a form submit url with javascript

Sometimes you might need to change the address a form submits to, it’s quite easy with javascript. One example could be if you - like me - from time to time is hit hard by comment-spammers. Changing the submit URL on the form with javascript, makes it at least somewhat harder for “automated brainless robots” to kill your site in a spam attack.

The recipie below is a very simple example, but so far it seems to work quite well:

<script type="text/javascript">
function doSubmit() {
  var target1 = 'hello';
  var target2 = 'world';
  var target3 = '.cgi';

  var supertarget = target0 + target1 + target2 + target3;
  var theForm=document.getElementById("theForm");
  theForm.action = supertarget;
  theForm.submit();
}
</script>

<form id="myForm" method="post" action="some\_fake\_address.php">
<input type="text" name="dummy field" value="">
<input type="button" onclick="javascript:doSubmit();">
</form>

Don’t use Ajax blindly

GMail and other web applications have adopted a new technique coined Ajax (by Adaptive Path). It brings web applications a step away from the stateless web and closer to real applications. It’s harder to built applications with the applications, but it’s hot – and the most recent release of Rails (for Ruby) promises to make it much easier to do Ajax applications. Before you do too many Ajax applications, do think for a second. Tadalist is a great example which could have been so much better with less Ajax code. The application rocks in a browser – if you’re at a full computer where the browser knows about and fulfil the requirements posed by the Ajax technique.

Client side Progress bar

Providing feedback in user interfaces is quite important. If you have a task, which may take a while, one of the ways to signal to the users, that the system is working, is by displaying a progress bar. If you don’t know how to make one, we do have one available for you.