I want easy access to common namespace URIs

Submitted by Thomas Wilburn

Every time I author SVGs using JavaScript—without a library—I end up having to look up the SVG namespace URI (and others) to make sure that the elements I create behave properly. It seems like an enormous oversight that I can’t access the “http://www.w3.org/2000/svg” URI (or other equivalent XML namespace URIs) from, say, a static property on the SVGElement constructor or something. Instead I have look it up, or (if I’m feeling clever) find some way to trick the parser into handing me a properly constructed SVG element, all so the browser can see the magic 26 letters that will cause my paths to display.

Here’s an example of what I could do if the browser has a set of predefined constants available to use:

// if NS_SVG was a global constant defining the SVG namespace URI
var image = document.createElementNS( NS_SVG, 'image');

Here’s what that might look like if the namespace was defined on the element representation instead:

const NS = SVGElement.NAMESPACE_URI;
var svg = document.createElementNS( NS, 'svg' );

Please, please, create a common way that I can access these and other namespace constants directly in my scripts. I don’t have to look up the exact integers that represent WebGL constants and we successfully simplified the HTML doctype… I have confidence that we can make this happen.

Tagged
JavaScript