File paths, chrome, and XUL

Filed under:Computing, Create It, Javascript — posted by Nic "RedWord" Smith on June 1, 02008 @ 1:34 AM

I was working with XUL and Javascript yesterday, and I ran into some interesting problems. I’ve set up my chrome so that the project I’m working on in Eclipse is available in Firefox as “chrome://twodee/content/exampleScreen.xul”. I wanted a function I wrote to accept a filename as an argument, but no matter what I tried, a relative path wouldn’t work.

Well, of course not. You can’t just arbitrarily go from chrome-based URLs to file locations. Tossing a chrome URL into a function that’s expecting a file path is going to have some bad results. Specifically, “not a file” exceptions.

So, this works:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="2DYouSee" title="Demo &quot;Game&quot;"
	xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
	onload="Start2D('C:\\Users\\Nic\\workspace\\2DYouSee\\content\\demo\\gameworld.json.txt')"
	width="650" height="490">
	<script src="io.js" />
	<script src="json.js" />
	<script src="twodee.js" />
	...
</window>

This does not:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="2DYouSee" title="Demo &quot;Game&quot;"
	xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
	onload="Start2D('gameworld.json.txt')" width="650" height="490">
	<script src="io.js" />
	<script src="json.js" />
	<script src="twodee.js" />
	...
</window>

More scheduler stuff

Filed under:Computing, Create It, Javascript — posted by Nic "RedWord" Smith on January 27, 02008 @ 12:07 AM

For anyone that’s interested in looking at it: My Scheduler, Source Code. Be forewarned: it’s ugly.

The Scheduler is Done

Filed under:Computing, Create It, Javascript, design (visual style), time management — posted by Nic "RedWord" Smith on January 23, 02008 @ 2:58 AM

…or, at the very least, it’s usable. Here’s a screenshot:

Chrono, my personal scheduling applet

The earlier screenshot that I posted was launched with window.openDialog, but I’m still having trouble getting Chrono to launch in its own window so that everything “just works” — so instead, I’ll be using it from inside Firefox, which I have open most of the time anyway. I even have a bookmark to go to it right away. I figured that I’d go over everything I’ve done, and describe what I’ve learned and what I could do to improve this project (but probably won’t). (more…)

Creating a new personal scheduler:

Filed under:Computing, Create It, Javascript, design (visual style), time management — posted by Nic "RedWord" Smith on January 16, 02008 @ 2:27 AM

I’ve been busy working on my 2008 theme, “Create it” — long fascinated by the power of XUL, I’ve decided to use it to improve my personal scheduling system. Here’s a screenshot of what I currently use, an OpenOffice.org spreadsheet:

My Scheduling Spreadsheet

And, here’s what I’m planning on replacing it with, a Javascript-automated, XUL scheduler:

XUL Scheduler Screenshot

Pretty spiffy. No, it’s not exactly Sunbird, but it could possibly make a decent Sunbird extension. One of the things I’ve had a lot of difficulty getting my mind around are the various security restrictions and variable scope oddities — I spent a lot of time trying to determine where my user settings (and by “extension” my chrome directory) actually were, since I’ve moved the same Firefox installation across three computers already.

Undefined vs. “Undefined”

Filed under:Javascript — posted by Nic "RedWord" Smith on April 24, 02007 @ 10:19 PM

There is no built-in equivalent of PHP’s isset() function in Javascript. Searching the internet for ways to work around this, you’ll see a lot of people recommend testing the (possibly undeclared) against undefined. In Venkman/Firefox, there is some unusual behavior here, which I haven’t seen mentioned elsewhere.

First of all, the recommended code:

var foo = 1;
if (foo === undefined)
{alert(”No foo”);}
else
{alert(”foo”);}

This will pop-up a window that says “Foo.”

Compare this to the following:

if (bar === undefined)
{alert(”No bar”);}
else
{alert(”bar”);}

This code will generate an error (”bar is not defined”) in Firebug, and presumably in the default debugger as well. Adding a single line fixes this:

var bar;
if (bar === undefined)
{alert(”No bar”);}
else
{alert(”bar”);}

This pops up a dialog that says, simply, “No bar.”

Rather than worry about an explicit declaration of a variable, you may try some code that uses typeof() to test if a variable exists or not. But, there’s a pitfall that I ran into — the following code is not correct.

var bar;
if (typeof(bar) === undefined)
{alert(”No bar”);}
else
{alert(”bar”);}

This code will actually output “bar”! It turns out that typeof returns the string “undefined” rather than the value undefined. So, in order to perform a test that should work right “no matter what” the code’s that actaually needed is:

if (typeof(bar) === “undefined”)
{alert(”No bar”);}
else
{alert(”bar”);}

This will output “No bar” as expected, with or without a preceding “var bar;”



image: detail of installation by Bronwyn Lace