Why defensive programming is important

The situation:
A flash movie that uses the ExternalInterface to receive data from the LMS through JavaScript.
Receiving the data just fine.

The issue:
The movie freezes as soon as the data is received.

The solution:
When the course initially launches the lesson_location is undefined. Apparently Flash freaks out when setting a variable to an undefined value through the ExternalInterface, so its as simple as setting the lesson_location to 0 in the JavaScript if it is undefined.

Labels: , , ,

A Missing Utilities Method

It has been brought to my attention that the Utilities object in my book is completely missing a method. Anyone who has had an issue related to the Utilities.getXY method, I apologize, please paste the following method into your Utilities file.

Utilities.getXY = function(el)
{
var x = el.offsetLeft;
var y = el.offsetTop;
if(el.offsetParent != null)
{
var pos = Utilities.getXY(el.offsetParent);
x += pos.x;
y += pos.y;
}
return {x: x, y: y}
}


The source code for the book will be updated on Sams web site ASAP.

Labels: , ,

Firebug 1.0 Beta

Joe Hewitt has been working hard to provide developers with a new version of Firebug. Check out the official product web site: www.getfirebug.com. As if the existing version wasn't amazing enough, the new beta version 1.0 simply looks incredible. Here is a list of some new features:

CSS editing
Network load timing
JavaScript profiling
Command line autocomplete
Debugger watch lists
DOM editing
Separate window support

Great job Joe, you make my daily life so much easier. I would certainly pay the $25, but I do agree that you should keep the existing version free.

Labels: , , ,

JavaScript Tween Objects

Check out this JavaScript animation engine. For anyone familiar with the Flash ActionScript Tween classes this library looks just as easy to use. Happy tweening...

Labels: , , , ,

Free Sample Chapter

A free sample chapter from my book Ajax for Web Application Developers is now available for download on my web site. Chapter 16, The Observer Pattern covers how to implement an error management system in your Ajax web applications by utilizing the Observer Pattern.

Ajax for Web Application Developers
ISBN: 0672329123
Copyright 2007 by Sams Publishing
www.samspublishing.com

Labels: , , ,

How to Keep Drop-Downs on Top

I recently had a request related to a previous post title Keeping the Drop-Downs on Top asking me how to implement the method that I was showing. I have decided to post a tutorial that explains how to use this method in conjunction with a drop-down menu in order to show just how to keep drop-downs above HTML form select elements.

We will start by adding the addIframe method to a Utils object:

<script type="text/javascript">
var Utils = new Object();
Utils.addIframe = function(id, el)
{
if(document.getElementById(id) == null)
{
var _iframe = document.createElement("iframe");
_iframe.src = 'about:blank';
_iframe.scrolling = 'no';
_iframe.frameborder = '0';
_iframe.id = id;
_iframe.style.position = 'absolute';
_iframe.style.border = "none";
if(el)
{
document.getElementById(el).appendChild(_iframe);
}
else
{
document.body.appendChild(_iframe);
}

}
return document.getElementById(id);
}
</script>


Once we have the object created we can get started with our drop-down menu, this menu will not be fully-functional, as I simply want to show you how to keep it above other elements. We will start by creating the HTML:

<div><a href="#" onmouseover="Menu.show('myiframe', 'menu');" onmouseout="Menu.remove('myiframe', 'menu');">Menu Title</a></div>
<div id="menu" style="display: none;">
<a href="#">Menu option one</a>
<a href="#">Menu option two</a>
</div>

<div class="content">
<select>
<option>An Option</option>
</select>
</div>


Now we have a menu title and two menu options, this will become our drop-down once we add the Menu object that we are calling from the mouseover and mouseout of the title. Below this drop-down code is the HTML select list wrapped in a div named content. Before we create the Menu object we will need to set a few CSS properties for these elements in order to get it all to render properly:

<style type="text/css">
#menu
{
position: absolute;
z-index: 2;
border: #333 1px solid;
}

#menu a
{
display: block;
}

.content
{
position: absolute;
}
</style>


This CSS will set the positions of the menu and the content to absolute so that the drop-down does not push the content over as it would in a relative position. Next we set the z-index of the menu to a higher depth than content and we add a display of block to the menu options in order to get them to stack.

Now that this is down we will create our Menu object and see it all in action. Below is the code:

<script type="text/javascript">
var Menu = new Object();
Menu.show = function(iframeId, menuId)
{
var menu = document.getElementById(menuId);
menu.style.display = '';

var _iframe = Utils.addIframe(iframeId, menu.parentNode.id);
_iframe.style.backgroundColor = '#fff';
_iframe.style.width = menu.offsetWidth +"px";
_iframe.style.height = menu.offsetHeight +"px";
}

Menu.remove = function(iframeId, menuId)
{
var _iframe = document.getElementById(iframeId);
if(_iframe.parentNode != null)
{
_iframe.parentNode.removeChild(_iframe);
}
document.getElementById(menuId).style.display = 'none';
}
</script>


This object has two methods, show and remove, pretty self-explanatory... The show method takes a unique id for the iframe and the id of the drop-down menu that you want to display. It then calls the addIframe method, sets its background color and its width and height relative to the menu in order to keep it hidden behind it. The remove method takes the same two parameters, but removes the iframe from the document.

Labels: , ,

HTML to String via JavaScript

If you ever receive an HTML string as a response from the server and try to write it to the page via JavaScript you'll find that it will not display it as a string. This is because the JavaScript parser is trying to render it instead of simply writing it. In order to work around this issue you can use the following method. Here's an example:

var html = "<body>My Title</body>";
document.write( htmlToString(html) );
function htmlToString(_html)
{
return _html.split('<').join("&lt;").split('>').join("&gt;");
}

Labels:

Designing with Code: Creating a Resizable Interface

I am hosting the Web Design Resource Center on InformIT again this week. My new column covers resizing a web application GUI with CSS and JavaScript. The column focuses on a CSS implementation, then it explains some of the limitations and how to work out the kinks with JavaScript. The column is called Creating a Resizable Interface, it features a sample and source code download. Enjoy.

Labels: , ,

Designing with Code: Providing Feedback

As of today, I am hosting the Web Design Resource Center on InformIT. My new column covers object-oriented feedback handling for web applications from a design perspective. The column is focused on Ajax web applications because the object that we create to handle feedback is a JavaScript-based object. The column is called Providing Feedback and features a sample and the source for the object that I cover. Enjoy.

Labels: , ,

Keeping the Drop-Downs on Top

Have you ever run into a depth problem with drop-down menus in Internet Explorer, everybody's favorite browser "chuckle"? If you have ever built a drop-down menu then I am sure that you have had this issue and it is not an easy issue to work around, until now...

If you have had this issue in the past you have probably found developers using the iframe hack, which renders an invisible iframe below the drop-down menu and brings it to the highest depth level, even above select lists in forms! This is a great solution, although, the solutions that I have found have been elaborate and complicated to implement. Therefore, I have created the method below named addIframe, which simply takes two parameters and renders the iframe for you, allowing much more flexibility from a development standpoint.

function addIframe(id, el)
{
if(document.getElementById(id) == null)
{
var _iframe = document.createElement("iframe");
_iframe.src = 'about:blank';
_iframe.scrolling = 'no';
_iframe.frameborder = '0';
_iframe.id = id;
_iframe.name = id;
_iframe.style.position = 'absolute';
if(el)
{
document.getElementById(el).appendChild(_iframe);
}
else
{
document.body.appendChild(_iframe);
}
}
return document.getElementById(id);
}


The method takes two parameters, the first is a unique id for the iframe that you will be creating. This allows you to later reference the iframe by id in order to move it, resize it, etc. The second parameter is a optional parameter which expects an element id for any element in which you want to append the iframe to, if this parameter does not exist it is simply appended to the document body. After the iframe is created it will be returned and can then be manipulated as I mentioned earlier. This is where you will want to tell it where to go and what size to be. Enjoy...

Labels:

XSL Variables in JavaScript

Recently I have been working on a web application that has an XSL front-end. I have been integrating Ajax widgets and other DHTML objects with the XSL and up until now I have had to create some work around code to retrieve XSL variables with JavaScript. After much frustration with the work around I have discovered that it is possible to write the variables directly to JavaScript as I am doing in the following example.

<xsl:template name="sampleTemplate">
<xsl:param name="sampleParam" select="''"/>
<script type="text/javascript">
alert( '<xsl:value-of select="$sampleParam"/>' );
</script>
</xsl:template>


I hope this saves some of you the time I spent creating work arounds.

Labels:

JavaScript Query String

Here's a great way for a front-end developer to retreive a query value without using a server-side language. This method simply takes a query key and returns its value if it exists in the current URLs query string.

document.getQueryValue = function(key)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for(var i=0; i<vars.length; i++)
{
var pair = vars[i].split("=");
if(pair[0] == key)
{
return pair[1];
}
}
return null;
}

I like to try and use a server-side language to accomplish this whenever possible, but there are definetely times that this comes in handy. My word of advice is to use it wisely...

Labels: ,

Mouse Coordinates

I feel like I've written the same mouse coordinate code a million different times, in a million different ways. I've finally created a simple JavaScript method to solve this issue.

document.onmousemove = function(evt)
{
if(!evt) { var evt = window.event; }
document.mouseX = (evt.screenX == null) ? evt.clientX : evt.screenX;
document.mouseY = (evt.screenY == null) ? evt.clientY : evt.screenY;
}

With this method in your document, all that you need to do is simply call document.mouseX or document.mouseY in order to get the current coordinates at any given time. Enjoy!

Labels: ,

Ajax on Amazon

My first release on Amazon
I am thrilled to say that my first book has officially been listed on Amazon: Ajax for Web Application Developers! The book is due out in late October and covers advanced Ajax topics for web application developers. The book will cover everything on Ajax from creating an engine and components, to connecting with a database with various languages.

Labels: , ,