SVG:Namespace: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
Line 14: Line 14:
   
   
  </svg>
  </svg>
= use namespace aware methods in javascript=
= use namespace-aware methods in javascript=


when using js, make sure you use the namspace aware methods like  
when using js, make sure you use the namespace aware methods like:


*getElementsByTagNameNS()
*getElementsByTagNameNS()
Line 25: Line 25:


svg elements are in the svg namespace. svg attributes are in the null namespace.
svg elements are in the svg namespace. svg attributes are in the null namespace.
for example, to dynamicly create an <image> element, do it like this:
for example, to dynamically create an <image> element, do it like this:


  svgns="http://www.w3.org/2000/svg"
  const svgns="http://www.w3.org/2000/svg";
  xlinkns="http://www.w3.org/1999/xlink">
  const xlinkns="http://www.w3.org/1999/xlink";
  newIM=document.createElementNS(svgns,"image")
  var newIM=document.createElementNS(svgns,"image");
  newIM.setAttributeNS(null,"width",100)
  newIM.setAttributeNS(null,"width",100);
  newIM.setAttributeNS(null,"height",100)
  newIM.setAttributeNS(null,"height",100);
  newIM.setAttributeNS(xlinkns,"href","bild2.png")
  newIM.setAttributeNS(xlinkns,"href","bild2.png");

Revision as of 21:40, 31 May 2005

declare namespaces for svg and xlink

the namespace declaration for svg is required if you don't specify it, nothing will be rendered. the xlink namespace is required if you use <use/>, <image/>, <a/> ...

a simple SVG file may look like this:

<?xml version="1.0" standalone="no"?>

<svg xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink">
 <title>title of drawing</title>

</svg>

use namespace-aware methods in javascript

when using js, make sure you use the namespace aware methods like:

  • getElementsByTagNameNS()
  • createElementNS()
  • setAttributeNS()
  • getAttributeNS()
  • ...

svg elements are in the svg namespace. svg attributes are in the null namespace. for example, to dynamically create an <image> element, do it like this:

const svgns="http://www.w3.org/2000/svg";
const xlinkns="http://www.w3.org/1999/xlink";
var newIM=document.createElementNS(svgns,"image");
newIM.setAttributeNS(null,"width",100);
newIM.setAttributeNS(null,"height",100);
newIM.setAttributeNS(xlinkns,"href","bild2.png");