Archive for the ‘JavaScript’ Category
* QUnit Server
Posted on March 1st, 2010 by Dave Johnson. Filed under Components, JavaScript, Testing.
QUnit is a great little JavaScript test framework written by none other than John Resig of jQuery fame.
I love how easy it is to write tests, especially asynchronous ones, with QUnit and so was quick to start using it for my JavaScript tests.
The one thing that I found it lacked was support for continuous integration products like Hudson, CruiseControl or Continuum. So I figured that I would change that.
For a long time I have been using JsUnit along with JsUnit Server to merge in client side JavaScript test results with a server side test integration workflow. Essentially, JsUnit Server is a Jetty server and JsUnit posts back a form with test results to that server, which logs the results to standard JUnit formatted test results files. Those files can then be read by most continuous integration products and included in their reporting.
What I decided to do was hack QUnit so that it too could submit test results back to the JsUnit Server in the same format that JsUnit submits results and could therefore continue using the same JsUnit Server workflow and simply change the client JavaScript test framework to QUnit. It worked like a charm!
I wrote a couple of simple function that in the background dynamically create an HTML form in the QUnit test page and add the results to the form as each test is run. When the tests are complete the page submits back to the JsUnit Server and the test results are logged as they were when I was using JsUnit.
It is some pretty simple changes but does make using QUnit that much better, especially if you were hesitant to switch just because the continuous integration support was not there for QUnit.
* HTML 5 and sessionStorage
Posted on February 19th, 2010 by Dave Johnson. Filed under JavaScript, quirks.
We came across a strange bug today that was being caused by our recent addition of sessionStorage to our application.
The Mozilla Developer Center says this about sessionStorage:
This is a global object (
sessionStorage) that maintains a storage area that’s available for the duration of the page session. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated.
Emphasis mine.
What we found was that of the three different browsers that currently “support” sessionStorage, there are three different implementations. Go figure.
I looked at a few different ways of opening new windows or tabs and those included anchor tags with target values of “” and “_blank”, window.open() JavaScript calls with different targets, and context menu open in new window or tab.
Internet Explorer 8 is furthest from the spec, as one might expect. No matter how you opened a new window from the page, the sessionStorage is preserved in the new window.
Firefox is only slightly better in that it does not preserve the sessionStorage in new windows when the new window is created by choosing open in new window / tab from the context menu on an anchor.
Safari is more or less like Firefox except that it does not preserve the sessionStorage when the target on an anchor is “_blank”.
In short, if you are expecting that sessionStorage is tied to a single browser window then think again.
* JavaScript Pub Sub
Posted on April 28th, 2009 by Dave Johnson. Filed under JavaScript, Uncategorized.
I was talking to someone the other day about the observer pattern in JavaScript and I was reminded about this JavaScript method that is part of Complete UI.
nitobi.Event.prototype.subscribeOnce = function(method, context) {
var guid = null;
var _this = this;
var func1 = function() {
method.apply(context || null, arguments);
_this.unSubscribe(guid);
}
guid = this.subscribe(func1);
return guid;
}
subscribeOnce is a special sort of event subscription that rather than being being executed every time the event fires, the handler function is only executed once and then automatically unsubscribed from the event. It is obviously most useful for initialization stuff in JavaScript. There is some more info on JavaScript pub sub over on Ajaxian too.
* 13 Tips For A More Accessible Web
Posted on November 17th, 2008 by Dave Johnson. Filed under AJAX, Accessibility, JavaScript.
There are few web projects that I have worked on of late where accessibility has been a high priority. Of course having worked on the Complete UI Ajax framework that is generally used to build internal web applications we were always trying to make our components as accessible as possible, particularly through the use of the keyboard.
In general through there are a few simple things that you can do to make you web applications far more accessible to your users that may use screen readers like JAWS or have to divert from normal computer settings like just using larger than normal fonts or even using high contrast mode.
- Remember if something can get focus through the tab key, users with screen readers will find it and the screen reader will describe it to them.
- The first is something that I am sure you have heard a thousand times and may or may not have listened. Don’t use tables for layout. I think that I saw this great site about it through Dion. Amongst the many arguments against using tables for your layout is that they have semantic meaning for
screen readers - that meaning is course that there is some tabular data to follow. So you can imagine it can be confusing when tables are found without any tabular data! - When a screen reader finds a table, users generally assume that is has a header row with column headings - so use the <th> tag to specify the column headers and maybe even put that entire row in a <thead> element.
- Use the unordered (<ul>) and ordered lists (<ol>) when appropriate. Unordered lists should even be used for things like menus by setting the display to inline and the list-style-image to something to get rid of the normal bullet.
- Always set the alt attribute on <img> tags. When the image is inside of an anchor this attribute should describe the image as the anchor will bring focus to that HTML. If it is just an ornamental image then leave the value of the alt attribute empty rather than ignoring it (this was demanded by a client due to bugs in certain browsers / screen readers).
- Use CSS for all images. The previous issue with image tags that are purely ornamental should not happen since you should be using CSS to set background images on your HTML elements. Not to mention the fact that those images should be sprited using something like SmartSprite and so you have to use CSS.
- Form elements should always have an associated <label> element with the for attribute pointing to the element that the label is for.
- Try to put dynamic HTML (drop down menus etc) into the HTML tree where they appear
visually such that tab index for focusable elements is maintained. If you generate a drop down but append it to the bottom of the HTML page and position so that it appears visually at the top of the page, you would have to set the tabIndex attribute on anchors in the menu to get the correct tabbing effect. - Don’t use tabIndex.
- Any element that has an onclick event on it should be an <a> tag (or at least enclosed in one). This will ensure that it is focusable and the user can then use the enter key to activate the click event. This is also a good reason not to use onmousedown or onmouseup events since those will not be captured by the keyboard. Similarly, if you use onmouseover and onmouseout to do anything that is not visual then you may consider having onfocus and onblur to do the same thing.
- If you have headings then use the heading tags! Semantics, semantics, semantics.
- Use relative sizings like em for your fonts and other dimensions and check out how the site looks as you change the font size.
- Put in a hidden anchor (position absolute, x,y -5000px) near the top of the HTML that contains a message like “Skip navigation” with an href pointing to an another anchor that will skip down to the actual contents of the page such that screen reader users can skip any redundant links and header information.
If you follow that simple list you should make your web sites and applications much more accessible.
For Firefox users I recommend the Firefox Accessibility Extension, which makes testing your pages pretty easy.
Anything to add? Please leave a comment below!
* Surfin’ Safari
Posted on October 9th, 2008 by Dave Johnson. Filed under AJAX, CSS, JavaScript, XML, completeui, quirks, safari.
For the past little while I have been busy fixing up Complete UI to work in Safari. The big problem for us in getting Complete UI working in the WebKit powered browser was that there was no client side XSLT JavaScript API until Safari 3.
So there were a few things I had to update to get things all working properly - and this more or less applies to Chrome now as well but I need to check on a few things.
XMLDocument::selectNodes
The first problem I came across was using the XMLDocument::selectNodes method. To get selectNodes working I had to implement my own NamespaceResolver which is usually handled for you by the browser.
I had to change the call to the XMLDocument:evaluate method a bit to look like this:
var oResult = this.evaluate( sExpr, (oContextNode?oContextNode:this), new MyNSResolver(), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
Where the MyNSResolver class essentially implements the lookupNamespaceURI method through a switch statement returning the appropriate URI for the specified namespace prefix. Pretty easy but kinda annoying to figure out!
function MyNSResolver() {};
MyNSResolver.prototype.lookupNamespaceURI = function(prefix) {
switch (prefix) {
case "xsl":
return "http://www.w3.org/1999/XSL/Transform";
break;
case "ntb":
return "http://www.nitobi.com";
break;
default:
return null;
}
}
XSLT Transformations
Then there was the actual act of performing an XSLT transformation. Doing the transformations was fine but getting the output node or string from the transformations - depending on if the <xsl:output /> was text, html or xml - was rather different than both Firefox and Internet Explorer. Go figure.
For transforming to an XMLDocument here is what I found to be the output of an XML transformation for the various output methods.
Text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<pre>innerText</pre>
</body>
</html>
XML:
<output>innerText</output>
HTML:
<html>
<body>
<output>innerText</output>
</body>
</html>
XSLT Parameters
There is also one difference in how the Safari XSLT processor deals with parameters. Rather than casting true / false to 1 / 0 the Safari XSLT processor does not recognize true or false values and requires the inputs to be 1 / 0. I have started setting all my XSLT params like this:
oXsltProc.addParameter("paramName", val+0, "");
Events and Focus
There were some other small issues that I came across regarding implementing rich keyboard navigation and events in general.
For example, in our Grid component I had to change the HTML element that we used to “focus” the Grid so that we could capture key presses from a <DIV> to an <INPUT>. Apparently you can’t call Element:focus on a <DIV> element from JavaScript but you can on an <INPUT> (even with a tabIndex set but I try to avoid those for accessibility reasons anyhow).
However, that was not my only problem with keyboard navigation. The other problem was that the focusable element (the newly created <INPUT> in this case) was still not getting focus due to a small code snippet like this:
oMyInput.focus();
cancelEvent(evt);
That second line where I cancel the event (wrapped nicely in a cross browser library call) somehow prevents the <INPUT> from getting the focus. Never figured out a workaround aside from a browser check before calling cancelEvent.
CSS
Finally, in some of our components we also generate CSS using XSTL and dynamically insert it into the page. For example, the developer defined column widths in a Grid produce dynamically generated CSS. However, WebKit does not seem to support the bulk creation of stylesheets neither using the document.createStylesheet method of Internet Explorer nor the hacky way of doing in Firefox by creating a <STYLE> element and setting the innerText. Instead Safari requires that you use the uber slow DOM API for adding rules to stylesheets
Miscellany
You will often see the “xml namespace prefix mapped to wrong URI” error when working with XSLT in Safari.
Be careful that you are using double quotes in your XSLT for attributes etc and only use single quotes in attribute values.
Of course you also have to be very careful about the nodes that you are transforming / selecting as you are also likely to get the “WRONG_DOCUMENT_ERR” fairly frequently.
Oh yah and depending on the application expect a lot of random “Stack overflow” errors!
* Complete UI Q4
Posted on September 26th, 2008 by Dave Johnson. Filed under AJAX, JavaScript, Nitobi, completeui.
Some big changes are coming in the Q4 release of Complete UI. Big!
Of course there is a slew of JavaScript bug fixes for all the components with Grid receiving the most attention for IE 8, Safari 3, Firefox 3 and Chrome.
There are a bunch of changes to the Java side of Complete UI with more great JSP and JSF code to make it even easier to use Complete UI in your Java project.
Finally, we have also made huge changes to the ASP.NET Grid and Combo code - and are working on the rest of the components as I am writing this! I think that anyone using ASP.NET will be very happy with the new direction of the ASP.NET code ![]()
We are shooting for an on time Oct 1 release but there is so much we are trying to fit in it might be a bit late - but it will be well worth waiting for!
* CommunityOne Presentation
Posted on May 8th, 2008 by Dave Johnson. Filed under AJAX, JavaScript, Patterns, communityone.
In addition to my slides, Andre recorded my CommunityOne presentation and put it on Blip. Check it out!
* CommunityOne Presentation
Posted on April 26th, 2008 by Dave Johnson. Filed under AJAX, JavaScript, Nitobi, Redmonk, Uncategorized, communityone, javaone.
So I have been accepted to give a presentation at CommunityOne, which occurs the day before JavaOne on May 5. I am going to talk about JavaScript + DOM patterns. I was going to focus on the JavaScript and DOM details around some common Ajax patterns such as those covered conceptually by Michael and Bill. Any ideas would be more than welcome!
Andre and I are also going to take part in the RedMonk CommunityOne festivities as much as possible as well which should certainly be a blast.
This is a bit of a digression but what a sweet picture. Ryan looks badass and Cote is all choked up!

* Ajax Functional Testing Fun
Posted on March 12th, 2008 by Dave Johnson. Filed under AJAX, JavaScript, Testing, selenium.
It seems that no matter how much you look for information about Eclipse and how to setup different types of projects, your version Eclipse is innevitably not the same version of the person who makes the tutorial and nor are you as much of an Eclipse geek as the writer of the tutorial. The same things will probably be thought about what follows ![]()
First of all I am doing this in Eclipse 3.3 WTP.
I started by creating a new Java project.

Which should have resulted in a basic project that looks something like this.

Then right click on the project root and choose “Properties” from the context menu (Alt+Enter). Go to the “Java Build Path” section of the properties. There you can include source files from elsewhere on your computer - so for example if you keep your test code elsewhere on your system you can include the source folder which will include your Java files in the Java project so that they can be built and run as a Java program while still keeping the files in your source control folder as well. This way you also avoid checking in Java projects all over the place that are useless for everyone else.

Now we add some external Jars. Find the Selenium Java client Jar and the JUnit Jar on your system and add them.

Now your project should be ready to go and you can create Selenium tests using the Java API and run them from Eclipse using JUnit. How sweet it is!

* Internet Explorer StyleSheet Quirks
Posted on February 19th, 2008 by Dave Johnson. Filed under AJAX, CSS, InternetExplorer, JavaScript, quirks.
I have spent the better part of the past week fighting with the Internet Explorer 31 stylesheets limit.

http://flickr.com/photos/cassidy/2461135/
It came up when, thanks to the great performance of our Grid component, a customer wanted to have about 35 grids on a single page for a very complex ordering system. Now as is usually the case, by the time that people come to us to help them they usually have a pretty firm business case for their application and don’t want to change the overall information architecture and the like - so we had to figure out how to get 35 grids running in Internet Explorer.
The problem arises from the fact that for a live scrolling grid where blocks of data are dynamically inserted into the page as the user scrolls, each grid needs its own stylesheet since the column widths and a few other parameters are defined on a grid/column basis and need to be changed globally in a lot of cases; for example, when a column is resized we need to be able to just change one CSS rule and have the widths of all the relevant HTML nodes get updated.
The worst part is that there is even a difference between IE 6 and IE 7 - w00t. In IE 7 it has no problem with just creating one stylesheet and continually appending or overwriting that stylesheet. So that was a pretty easy fix to have a global stylesheet instead of one for each grid. Then comes IE 6… in IE 6 it seems that you can’t even write to the same stylesheet object more than 31 times (or some number around 31 I am not really sure).
So what was the solution for IE 6? We essentially had to make a registry of all the components on the page that keeps track of which components were done initializing and then when all the components are ready create one huge stylesheet and write the contents only that one time. Failing to use this approach meant that IE 6 would just crash.
The other approach that we of course considered was using addRule on the stylesheet object to inject each of the CSS rules into an existing stylesheet rather than writing the stylesheet. I quickly learned that addRule is ridiculously slow in IE. Something like on the order of 30ms to add a rule. So ~35 grids * 50 columns per grid * 30ms per addRule = way too long. Retarded.
We now almost have everything working with > 31 grids. Joy.
Recent Posts
Archives
- March 2010
- February 2010
- January 2010
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
