Note that there are some explanatory texts on larger screens.

plurals
  1. POFirefox -- Dynamically embedding <svg> element in SVG
    primarykey
    data
    text
    <p>I am trying dynamically to append an <code>&lt;svg&gt;</code> element to an existing SVG island on an XHTML page (Firefox 3.6.3). And it is crashing the browser.</p> <p>Done manually, this works as expected:</p> <pre><code>&lt;svg xmlns="http://www.w3.org/2000/svg"&gt; &lt;svg xmlns="http://www.w3.org/2000/svg"&gt; ... &lt;/svg&gt; &lt;/svg&gt; </code></pre> <p>However, if you dynamically add this element using JavaScript, the browser crashes. Simple example:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;SVG island example&lt;/title&gt; &lt;script type="text/javascript"&gt;&lt;![CDATA[ function crash( ) { svgs = document.getElementsByTagNameNS( "http://www.w3.org/2000/svg", "svg" ); for ( var i = 0; i &lt; svgs.length; i++ ) { var e = document.createElementNS( "http://www.w3.org/2000/svg", "svg" ); svgs[i].appendChild( e ); } } ]]&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;svg id="mySVG" xmlns="http://www.w3.org/2000/svg"&gt; &lt;/svg&gt; &lt;button onclick="crash()"&gt;Crash Firefox&lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Interestingly, if I do a <code>getElementById</code>, it works <em>fine</em>. Interesting, but not particularly helpful in my situation since I'm storing pointers to <code>SVGDocument</code>s. Example:</p> <pre><code>function doesntCrash( ) { var svg = document.getElementById( "mySVG" ); var e = document.createElementNS( "http://www.w3.org/2000/svg", "svg" ); svg.appendChild( e ); } </code></pre> <p><strike>As far as I can tell, this is a Firefox bug.</strike> Does anyone have any insight into this matter?</p> <p><strong>UPDATED (solution):</strong> As stated below, the issue was the "liveness" of the <code>HTMLCollection</code> returned by the <code>getElementsByTagNameNS</code> call which I mistook for a native array (<em>tsk, tsk!</em>) A quick hackaround would be to either store the array length in a variable, if you're only appending. A better solution might be to copy the array contents to a native array, as described <a href="https://stackoverflow.com/questions/222841/most-efficient-way-to-convert-an-htmlcollection-to-an-array">here</a>. Here's an updated using that method:</p> <pre><code>function doesntCrash( ) { var svgs = document.getElementsByTagNameNS( "http://www.w3.org/2000/svg", "svg" ); // copy contents to native a static, array svgs = Array.prototype.slice.call( svgs ); for ( var i = 0; i &lt; svgs.length; i++ ) { var e = document.createElementNS( "http://www.w3.org/2000/svg", "svg" ); svgs[i].appendChild( e ); } } </code></pre> <p>Thank you Sergey Ilinsky for your quick response!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload