javascript - quand la fonction onload du svg se produit


Étant donné ceci :

<?xml version="1.0" standalone="yes"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100px" height="100px" version="1.1"
 xmlns="http://www.w3.org/2000/svg"
>
<text 
 x="20" 
 y="20" 
 onload="alert('load'); setAttribute('fill', 'fuchsia')"
 onclick="setAttribute('fill', 'lightgreen')"
 onmouseout="setAttribute('fill', 'black')"
>Load me</text>
</svg>

Je m'attendrais à voir du texte rose lorsque le SVG est ouvert. onclick et onmouseout fonctionnent comme prévu.

Cela n'arrive pas dans Firefox. IE ne peut pas l'ouvrir, point final.

Des explications ?

Réponse #1

Utilisez l'événement onload sur l'élément <svg>. Cela fonctionne bien sur tous les navigateurs.

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<svg onload="init(evt)" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gui="http://www.kevlindev.com/gui">
    <script>var info,infoElem;

    function init(e) {
        if ( window.svgDocument == null )
            svgDocument = e.target.ownerDocument;

        infoElem = svgDocument.getElementById("info");
        infoElem.setAttributeNS(null,"fill", "fuchsia");
    }

    function changeColor(c){
        infoElem.setAttributeNS(null,"fill", c);
    }</script>
    <text id="info" x="20"  y="20" onclick="changeColor('lightgreen')" onmouseout="changeColor('black')">Load me</text>
</svg>

Réponse #2

Cela a fonctionné pour moi

//snip...

    <svg width="100px" height="100px" version="1.1" onload="alert('load'); setAttribute('fill', 'fuchsia')"
     xmlns="http://www.w3.org/2000/svg"
    >

//snip...