Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
Programming

Journal Journal: Reading Files in Internet Explorer with JScript

Internet Explorer is a strange beast. I happened to be writing a DHTML Behavior to fix up some browser incompatibilities. One thing it had to do was download a CSS file and store it as a string for interpretation later. If ActiveX was enabled, then it is simply a matter of using the (relatively cross browser) XMLHTTP object:

function readFile(url) {
var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
httpRequest.open("get", url, false);
httpRequest.send();
return httpRequest.responseText;
};

In a perfect world, I'd use this one. On the other hand, in a perfect work everyone would be using Mozilla to browse the web. ;-) My script must work even if ActiveX is disabled, which complicates things. There is also a built-in download behavior, but it still doesn't work if ActiveX is turned off. Custom DHTML Behaviors fortunately still work as long as active scripting is enabled, so not all hope is lost.

Microsoft jumped on the XML bandwagon early while developing Internet Explorer 5.0. Their first attempt used something called xml data islands. Instead of loading an XML document directly into the browser, they used an extension to the common SGML version of HTML - an <xml> element. It's sort of like <script> for XML: it can be wrapped around the XML or just link to an external document.

It also exposes a limited interface to an XmlDocument class. Since the object is represented as a SGML element, we can create one with the document.createElement("xml") factory method. There's also a nice side effect, where its interface still works even before being added to the DOM tree. (Some elements, like <iframe>, must be added before being used with penalty of an Unknown Error from IE.) Thus we could load any XML data - using the parts of the XmlDocument interface that work - with the following function:

function readFile(url) {
var newFrame = document.createElement("xml");
newFrame.async = false;
if (newFrame.load(url)) return newFrame.xml;
else return "";
};

Loading the CSS file now would result in an error since it isn't an XML file. (This strict parsing rule - unlike HTML - is actually the correct behavior according to the spec). Unfortunately, it can't be fixed without the ability to resolve external entities, which isn't available for security reasons. The solution is a compromise by adding a few lines to the files so IE thinks they are valid XML documents. This doesn't mean that the CSS files have to be invalid. Thank the W3C deities for adding comment declarations to the core CSS grammar for compatibility with SGML HTML. Internet Explorer's xml processor returns the comments (even though it doesn't half to), so the style sheet data can be inferred from the following:

Beginnning:
<!--
Ending:
/* --> <style/> <!-- */ -->

The SGML-style comments hide the CSS-style comments and the rest of the code. Retrieving the valid CSS is as simple as reading the value of each XML node:

function loadFile(url) {
var nodeText = "", cssText = "";
var newFrame = document.createElement("xml");
newFrame.async = false;
if (newFrame.load(url)) {
var children = newFrame.childNodes;
var numOfChildren = children.length;
for (var ii = 0; numOfChildren > ii; ii++) {
nodeText = children[ii].nodeValue;
if (nodeText) cssText += nodeText;
}
}
return cssText;
};

That's easy from the developer's point of view; however, it's an ugly ugly ugly hack for the user (CSS author). Depending on your requirements and taste, the following hider may be easier to stomach:

Beginnning:
<!-- /* --> <style> */
Ending:
/* </style> <!-- */ -->

Either case doesn't matter to the function above. Just be careful if you're having lunch. ;-)

P.S. I plan on adding this article to my program's documentation. Help editing it and constructive criticism would be much appreciated.

Note: A bug with /. puts a typo in the first code block. That should be "open" not "o pen". Cowboy Neil must hate me!

**IMPORTANT** THE ABOVE NO LONGER SEEMS TO WORK WITH INTERNET EXPLORER 6 SP 2. USE THIS PATTERN INSTEAD

Programming

Journal Journal: I need help writing documentation 4

I need advice with writing good documentation. Programming has been a hobby for over a decade, but I never really released anything to the world until now. I need to create a web site to support the project - and it is hard! Writing good documentation (writing good anything) seems to be a very involved process, and I feel out of my league. I'm looking for some good tips or resources to get started.

The project is an extension to Internet Explorer 5.5/6 to get some CSS Level 2 features working. It's a lot like Dean Edward's IE7 with slightly different design goals. I wasn't yet going to release it publicly, but some people have been asking (and I caved ;-).

Any suggestions?

Businesses

Journal Journal: Open Source does NOT threaten capitalism 1

Note: This is an edited repost from a reply to a particular yahooish comment. It is also a little bit of a jumbled rant, so apologies in advance. :-)

I get upset when people think Open Source software is communistic. It has nothing to do with threatening capitalism. In fact, I would argue that OSS is in the spirit of capitalism!

At its core, capitalism is about minimizing costs of creating and distributing resources through competition. Risk is one of those costs, yet OSS reduces those risks when compared with other proprietary solutions. No large-scale software projects are free of bugs, so a risk of using any software is the stability of the creator. The firm may go out of business; the software may be discontinued; or the firm may charge more over time. OSS can be developed indefinitely, even after the original creators can not be relied upon to maintain it at a reasonable price. That is the value of the source code.

What are the advantages of open, Free (as in Freedom) code? One problem with any software includes the development costs of programmers and the project's administration. A firm could hire programmers to create code under any license (open or closed) requested, but these costs can be huge for large-scale projects (the projects from the above paragraph). It makes sense that if programming resources could be spread throughout the marketplace, then an individual firm's cost of software maintenance could be reduced to feasible levels. But how does one firm encourage its competitors to cooperate with it? The answer are open standards. For over two hundred years, it has been shown that common, open standards - such as money values, weights and measures, or product specifications, among others - help reduce costs as a whole in a market. (Note that when the price of a product is determined more by demand than supply, then this translates into higher profits.) By allowing the source code of a software application to be open and free from abuse by any one firm (hence, Free), then the code gains the advantages of open standards. The contributions by one firm can't be stolen by another firm, and additional restrictions and costs - like royalties or other fees - can't be levied by any one firm. Hence, Free OSS is more economical than proprietary closed source software for large, complicated projects.

But does the sharing of code go against capitalism? No! There is competition between open source software projects (such as Gnome vs. KDE, Mozilla vs. Konqueror, Linux vs. BSD). The market forces help streamline the administration/design of any individual projects, or it will be dominated by others and eventually become irrelevant. The users of OSS lower their individual costs and become more competitive in their respective markets. (For instance, OSS lowers IBM's costs of maintaining software. They can't really sell it, but IBM gets their money back - and more - by selling their experience with the software!) Thus, Open Source software lets firms be more competitive.

Note that some software projects are either too small or too big to be economically Open Source. The market may not be large enough to support the costs of the program (such as large databases like encyclopedias, or complex programs like robotic control software). On the other hand, there might not be enough interest in other projects (such as program documentation, which is generally poor in the open source community). Still other types of software may be the product (such as entertainment software like games or web-logging tools, or other commercial software such as p2p clients). Despite these limits, a lot of software could be economically Free OSS in a capitalistic society. Of course, others could be fueled by volunteer efforts for which we should be more grateful.

Please accept my apologizes for this rather long, rambling rant. I get worked up sometimes: I hope you understand. :-)

Slashdot.org

Journal Journal: First Post!

Ya! I finally got an article accepted! Thank you Hemos. For the record, here's a list of my rejections:
  • 2003-01-27 13:56:52 DDR-based IDE bus (askslashdot,hardware) (rejected)
  • 2003-02-07 14:49:38 AMD Speed Ratings Formula (articles,amd) (rejected)
  • 2003-02-12 15:32:40 PSA: Mozilla 1.3 beta out!!! (articles,mozilla) (rejected)
  • 2003-03-24 15:39:22 Open vs. Closed Software Talking Points (articles,programming) (rejected)
  • 2003-04-06 15:32:32 Wrox is going out of business (articles,biz) (rejected)
  • 2003-05-05 05:47:00 WordML well documented, generated with XSLT (articles,microsoft) (rejected)
  • 2003-09-24 22:43:30 When is it legal to download songs? (askslashdot,ent) (rejected)
  • 2004-01-26 07:44:20 "X" Clipboard Annoyances a Thing of the Pa (articles,gnome) (rejected)
  • 2004-02-09 08:04:43 Spirit Grinds Adirondack, Looks for Iron (science,science) (accepted)
  • 2004-02-09 09:20:56 Mozilla FireFOX 0.8 released (articles,mozilla) (rejected)

Slashdot Top Deals

If Machiavelli were a hacker, he'd have worked for the CSSG. -- Phil Lapsley

Working...