2

Is there any way I can run jQuery under SVG instead of HTML?

I know about jQuery svg plugin but this is not what I want.

As for now the jQuery engine contains some statements like this:

div = document.createElement("div");
div.style.display = "none"; 

It fails with exception during jQuery initialization under SVG because SVG document is unable to create div element. And there are some other HTML-specific places in jQuery which fail in unpredictable places under SVG.

Is there any way to resolve this problem?

Volodymyr Frolov
  • 1,256
  • 5
  • 16
  • 25

1 Answers1

3

I hacked an old version of jquery around a bit, and it is possible to run it in svg-only mode with some modifications. Feel free to take the changes and do whatever you want with them. Here's a demo and here's the modified jquery sourcecode.

It's incomplete and probably has some issues if you want to use it in both HTML and SVG, but it's a start, and works to some extent at least.

Erik Dahlström
  • 59,452
  • 12
  • 120
  • 139
  • 1
    There was one more issue with classes of elements. Class selectors and methods didn't work because in HTML elem.className is a string but in SVG it is an object. String class is taken from elem.className.baseVal. I have patched newest (1.4.2) version of jQuery. One more issue is that SVG doesn't call initialization methods so I introduce jQueryInitialize and call it in onload. Here is a link: http://hgcoaxial.appspot.com/static/jquery-1.4.2.svg.js to the patched jQuery. And here is more sophisticated example: http://hgcoaxial.appspot.com/static/jquery-1.4.2.svg.js of jQuery under SVG. – Volodymyr Frolov Feb 20 '10 at 20:37
  • Thanks, almost works, only - how do I animate e.g. position of a circle? jQuery works with CSS attribs, and circle's `cx` / `cy` has no CSS counterpart IIUC. I've tried `top` but no effect (although does not throw). – Ondra Žižka Aug 16 '10 at 23:19
  • @Volodymyr, thanks for the link. Do you plan on keeping this updated? I have real use for this and would love to see it become official. – Courtney Christensen Feb 14 '11 at 18:10
  • @zourtney, thanks for feedback. Yes, I think about making it official, and keep it updated. But I relay need help on this, I don't have enough time to make it alone. – Volodymyr Frolov Feb 22 '11 at 15:17