Skip navigation.

Claws, fangs, fur...

...the bear essentials

Posts tagged with "Javascript"

Javascripted stylesheet switchers - necessary evil?

, , , ...

All web browsers that are built with ease of use in mind, sport accessibility options. Microsoft Internet Explorer has fared well in this respect, for as long as I can remember. Then Opera and Firefox started offering more. Microsoft wised up and added a stylesheet selector to its interface starting at MSIE8.

But, if you happen to be using Safari or Chrome, you're S.O.L. Apparently, the vendors don't care about user accessibility. No. They must hate you.

Why?

Because they don't offer a stylesheet switcher.

What's a stylesheet switcher?

If a web author is nice enough to think about the different machines on which their web page could be shown, the smart author offers the same well-formed web page in varying layouts. These layouts are called stylesheets. By setting the media attribute, a web author can determine whether a stylesheet should be loaded by a printer, a desktop web browser, a small-screen web browser, a big-screen projector, or something else.

Some authors offer different layouts for the same machine. Sometimes just to spice things up, and sometimes to make their web page easier to use under certain circumstances. Those stylesheets are called 'alternate stylesheets'. User-friendly web browsers recognise these, and offer a switch in their menu, to let the user switch between one layout and the other.

(The web authors know that this switch also comes in handy when testing their stylesheets for other media.)

A user-hating web browser fails to incorporate such a switch. Knowingly, they prevent the user from using something the web author has created to make their lives easier, resulting in the browser making the user's life harder.

Not good.

The web author then starts looking for ways to enable it anyway. I consider this evil: building into a web page, what the browser should've offered. We see other examples of such necessary evil: font size changers, rss feed icons, bookmarking facilities... all created to overcome browser deficiencies. Necessary evil.

Apparently, for stylesheets, the user-hating browsers seem Safari and Chrome. The way for the web author to get those browsers to offer a stylesheet switch to their users anyway, is by building it into their web page. This can be done using javascript.

All it seems to take, is look for the [rel=alternate stylesheet] and turn it into [rel=stylesheet]. (Edited to correct that: it also takes setting [disabled=false|true].) And the other way around, of course. And allow for some stylesheets to remain active while others are switched around.

I wonder what such switchers do to interface of the user-friendly browsers, that provide such a switch themselves...

Javascripts and standardized object and event models

, , , ...

So, we've got Ecmascript defining the language commonly known as Javascript. We've got W3C defining the DOM, the document object model, which represents an (x)html document loaded in a web browser, as well as the javascript interface to manipulate that document. And we've got the window model, which was developed before the more abstract DOM, but is still implemented and improved to this day.

So why can't browser vendors reach an understanding on how to do things? What possible advantage does it serve to make it harder on the web authors, to get stuff accomplished? I'm sure there's some benefit, beit monetary (doubtful, since in this case, the deviant is both open-sourced and free) or pride (also doubtful, since in this case, the deviant was rebuilt from the ground up), or something else (please enlighten me?).

What on earth am I talking about?

Events. Javascript events. More specifically: the click event, which happens when users click on a screen element, usually using a point-and-click device like a mouse, a pen, or their fingers.

What about it?

Coordinates. It seems very hard for browsers to let the web author know, what the coordinates are where the click was registered. And if we need that information to, for instance, drag an element around the screen, or collect the exact pixel information, we are faced with browsers that insist on doing things differently.

The other day, I added a colour picker to the Canvas Composition Studio.


Image: CCS Colour Picker

Based on the new colour picker in My Opera's style designer (which from the looks of it is part of the Yahoo User Interface library), it presents a swatch of colours and greys, from which the user can choose by simply clicking the desired colour.

In Opera, Safari, and Chrome, the click event holds the attributes offsetX and offsetY. This tells the web author exactly where the element was clicked. That coordinate can then be used for the Canvas method getImageData(x,y,width,height), which returns an rgba representation of the colour.

Firefox also returns supports the getImageData() method, also returning the rgba representation of the colour chosen. However, its click event does not hold the attributes offsetX and offsetY. Instead, it holds layerX and layerY. Yay, Netscape legacy. In itself, that isn't bad. A quick test for support of an attribute fixes the problem.

Not.

See, in the specific situation of the colour picker in the Canvas Composition Studio, layerX and layerY don't tell the web author where the element was clicked. As a matter of fact, layerY returns a value that puts it way outside of the clicked element...

Why? I don't know. All I know is that the coordinates provided by these attributes are useless. And the event also fails to provide any information as to how the coordinates are offset. So we're left to calculate the coordinate by ourselves.

To do so, we must match the event.clientX to the eventTarget.offsetLeft... which anyone would agree is (start sarcasm) totally obvious (end sarcasm) from cracking open the event and eventTarget objects...

Thank you, Firefox building team, for making this web author's job harder.

I haven't even tried catching this coordinate in Microsoft's excuse for a web browser, yet. Maybe that'll be easier, for a change.

Canvas Gradients and CSS3 HSL

, , , ...

What would you expect when you see this javascript?

/* assume context is an HTML5 Canvas 2d context */
var g = context.createLinearGradient(0,0,width,height);
g.addColorStop(0,'hsl(0,0%,0%)');
g.addColorStop(1,'hsl(0,100%,100%)');
return g;


Would you expect a gradient flowing from black to white, with grades of red in the middle? Me too!

For a yet unknown reason, all tested Canvas-supporting browsers decided to show only shades of grey. No red.

In order to see red, we actually need to add a specific colour stop between the two already present:

var g = context.createLinearGradient(0,0,width,height);
g.addColorStop(0,'hsl(0,0%,0%)');
g.addColorStop(0.5,'hsl(0,50%,50%)');
g.addColorStop(1,'hsl(0,100%,100%)');
return g;


But that, obviously, fails to show a fully saturated red anywhere in the gradient.

On a slightly related topic: why we need to pass the hsl() (or the rgb()) colour creator as a string beats me. Why not just add an hsl() function to your javascript parser? Only because javascript refuses to accept 0% and 100% as numbers? Well, then let us specify those as fractions between 0 and 1, just like alpha transparancy.

But no... that would make the notation inconsistent with the CSS3 notation... and we all know that the CSS3 colour module has been a fixed recommendation for years, right?

Not.

Testing how browsers react to changing element attribute values

, , , ...

Result: poorly.

http://www.omegajunior.net/code/tests/test_img_checkbox.html

Selecting a checkbox does select the checkbox... but it doesn't cause ANY tested browser to update the checkbox styling. To change the styling, authors must resort to scripting, like changing the class attribute using javascript.

This behaviour follows the HTML4 and CSS2 recommendations... which I consider quite disappointing. I'd love to see browsers react to attribute value changes by applying the CSS specified for the varying values. That way, less javascript is needed.

I can understand why browsers don't, and why their behaviour is recommended in HTML4 and CSS2: reacting to changes in several attributes for the same element may cause confusion and headaches for the author, let alone the browser programmers.

As if that ever stopped either of those groups from implementing something...

Opera Javascript Speed affected by chosen Opera Appearance Skin

, , , ...

The oddest thing just occurred. I tested the javascript speed in Opera 10 (build 1426), using my favourite javascript application (the YASS described in an earlier blog post), and a very crude way of benchmarking (counting seconds while waiting for the application to finish solving the given Sudoku puzzle).

With my Opera 9.63 configuration, my javascript application takes roughly 8 seconds to solve the puzzle. In comparison: MS Internet Explorer 8 requires roughly 15 seconds. To my amazement, the new Opera 10 required even more than that: nearly 24 seconds.

Now, for reasons only known to me, I performed that test before I switched Opera's Appearance Skin from Opera Standard to Windows Native (yes, I run a MS Windows XP Home edition on a rather fast desktop computer). After switching, something compelled me to run the test again... just to make sure I hadn't miscounted.

Lo and behold: it took the application 8 seconds again.

Huh?

So wait... the choice of the Opera Appearance Skin influences the speed of its javascript engine?

Apparently.

This might only be the case, due to my application echoing intermediate results back to the screen. However, that doesn't strike me as particularly far-fetched. Yes, it can probably run faster if the echo is done on interval, but oh my, showing the Opera Standard Skin slows down my javascript application by a factor of nearly 3...

I know some competitors who are going to love sneering at this...

Yet Another Sudoku Solver

, , , ...

Added to the Code section of the OmegaJunior.Net: our own method of solving Sudoku puzzles, as a way of simulating the human thought process in computer programming.

We didn't set out to solve any and all Sudoku challenges out there... just that we created a program that mimics one single solving strategy, which can and indeed does fail with some specially crafted challenges. That strategy is called the "3DRLI": the "3 Degrees Reduction and Lock-In Strategy". The article describes the strategy and sports a javascript implementation of a solver using that strategy.

You may wish to use an HTML 5 / CSS 3-compatible browser to appreciate the full experience of this article and the YASS. Opera 9.6 / 10 and Google Chrome 1 come to mind. Recent nightly builds of Firefox 3 look promising and sport the fastest javascript engine for this application, but fail to support the innerText-property and some HTML5 elements.

Some minor flaws in Opera appear when using Opera 9.6 or newer. For instance: we've employed WebForms 2.0 Number Inputs. (That's an HTMLInputElement with its type set to "number".) Those inputs sport a max and a min property. Guess which browser does allow setting that property and validating for it... but fails to limit the user's input?

Some minor display flaws in many a browser appear. We've employed HTML5's Figure element, sporting a Legend element. Non-HTML5 browsers (there are many, currently), see the Legend element, and conclude that it must necessarily be part of a Fieldset element, which obviously is missing. So their rendering engine automatically puts one in... which then fouls up the display. Especially Firefox 2 and 3 are troubled by this flaw. Other browsers show a tiny spec of border where they assume the Fieldset should start, as Fieldsets generally are delineated using borders. Firefox 2 and 3 however, insist on enclosing the entire rest of the article in a border.

Looks promising for HTML5 rendering, doesn't it?

Public preview of canvas implementation available

, , , ...

For the Pantheon Online Games site we have created an Altar Dress-Up application. You may have read about this in my previous blog post.

This application is now up for public preview. It heavily uses the new Canvas element that will be part of HTML5, and requires the use of Javascript. People using browsers that either have Javascript turned off or do not understand the Canvas element, are out of luck.

One eerie thing I found: all modern popular browsers know the Canvas element except for Microsoft's attempt at a web browser... but even though it does not know how to handle a Canvas element, it has no qualms creating one using Javascript methods. That makes my fall-back content rather useless... Choose, Microsoft, choose! You either know it, or you don't!

Edited to add another eerie thing I found: Opera 10.0 Alpha (yes... Alpha) duplicates the canvas displays. The duplicates are offset by a handful pixels. Sometimes the duplicates move, too, when other stuff is being moved around.

Edited to add an o.O.: the canvas ghosting seems to occur in several versions of Opera. We haven't heard the last of this problem!
Download Opera, the fastest and most secure browser