Script in SVG with IE

Discuss SVG code, accessible via the XML Editor.
svgTester
Posts: 3
Joined: Wed Jul 24, 2013 3:30 am

Script in SVG with IE

Postby svgTester » Wed Jul 24, 2013 3:42 am

Hello.

I have a Problem with JavaScript in SVG. It works fine in Firefox, but not in the Internet Explorer.
I just want to change a Symbol, but the Internet Explorer stop working if the Symbol is changed once. Only first changing of Symbol is working. Normally it should toggle by each click.
In function "toggle" is the comment "//alert("crazy");"...and the crazy thing is, that it works in the IE if you do this alert.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<script language="JavaScript" type="text/javascript">
<![CDATA[
var display = true;
function toggle(className)
   {
                                                                //alert("crazy");
            if(display == false){
               display = true;
               document.getElementById(className).setAttribute('xlink:href','#red');
            }
            else{
               display = false;
               document.getElementById(className).setAttribute('xlink:href','#green');
            }
   }
   
]]></script>
<defs>
   <symbol id="red">
      <rect width="25" height="25" style="fill:rgb(255,0,0);"/>
   </symbol>

   <symbol id="green">
      <rect width="50" height="50" style="fill:rgb(0,255,0);"/>
   </symbol>
</defs>
<a xlink:href="javascript:toggle('Rectangle');">
<use xlink:href="#red" x="100" y="100" id="Rectangle"/>
</a>
</svg>



I hope somebody knows an issue.

Thanks and Greetings

v1nce
Posts: 696
Joined: Wed Jan 13, 2010 4:36 am

Re: Script in SVG with IE

Postby v1nce » Wed Jul 24, 2013 7:29 am

probably a default event handler that reloads your svg

try to replace href with "javascript:('Rectangle');return false;"

svgTester
Posts: 3
Joined: Wed Jul 24, 2013 3:30 am

Re: Script in SVG with IE

Postby svgTester » Wed Jul 24, 2013 4:19 pm

Thank you for you answer, but could you write the code, what you what change? I don't know which href should be replaced.

svgTester
Posts: 3
Joined: Wed Jul 24, 2013 3:30 am

Re: Script in SVG with IE

Postby svgTester » Thu Jul 25, 2013 5:18 pm

Could you make an example? I'm not sure which href you would replace.

chriswww
Posts: 383
Joined: Fri Nov 19, 2010 3:04 pm

Re: Script in SVG with IE

Postby chriswww » Fri Aug 02, 2013 3:23 pm

The thing with javascript itself is you have to write the functions so that variables are not out-of-scope, when the javascript function is declared. I forgot what this is called but it's a feature of javascript that allows easy overloading of functions, even built-in ones. The upshot is that you have to treat the declaration of the function, when you're writing it, as though it was going to be executed at that point where it is declared. Otherwise you get weird errors or strange things happening. The standard practice is to write a function creation wrapper.
I think this stackoverflow post will set you on the right path http://stackoverflow.com/questions/1013 ... -javascrip

hulf2012
Posts: 716
Joined: Sat Nov 24, 2012 12:37 pm

Re: Script in SVG with IE

Postby hulf2012 » Sat Aug 03, 2013 12:22 am

Hello,

just to say that the code implementation seems different to this.
http://msdn.microsoft.com/en-us/library ... x#example2

I hope to see that you solved your question

Greetings
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.


Return to “SVG / XML Code”