0

I am wondering how to include jpg-images in SVG!

var ns = 'http://www.w3.org/2000/svg';
var svg = document.createElementNS(ns, 'svg');
svg.setAttribute('width', 800);
svg.setAttribute('height', 450);

var image = document.createElementNS(ns, 'image');
image.setAttribute('href', 'http://placehold.it/300x300');
image.setAttribute('x', 0);
image.setAttribute('y', 0);
image.setAttribute('width', 800);
image.setAttribute('height', 450);

svg.appendChild(image);
document.body.appendChild(svg);

Where is my mistake?

5
  • What's the issue? Works for me Commented Jan 18, 2017 at 19:41
  • do you see the image ?! Commented Jan 18, 2017 at 19:42
  • Yes, what browser are you using? Commented Jan 18, 2017 at 19:44
  • I am using Safari ... :( Commented Jan 18, 2017 at 19:44
  • so it turns out safari is having trouble rendering svg images ... In chrome there is no problem. ;-( stupid safari. Commented Jan 18, 2017 at 19:47

1 Answer 1

2

For Safari you'd need to change

image.setAttribute('href', 'http://placehold.it/300x300');

to

image.setAttributeNS('http://www.w3.org/1999/xlink', 'href', 'http://placehold.it/300x300');

This second way of doing things is Chrome/Firefox compatible too.

Safari may support your way in the future as it's part of the upcoming SVG 2 specification, the second way is the SVG 1.1 way to do things.

Sign up to request clarification or add additional context in comments.

2 Comments

I also needed this fix to make it work on Firefox 50.1.0 running on Ubuntu 14.04.
Firefox 51 will be the first release to support bare href attributes. Only a few days to wait now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.