File paths, chrome, and XUL

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>

Leave a Reply

You must be logged in to post a comment.