I am trying to add an xlink:href attribute to an SVG element from JS, but it's not working.
Here is my code:
test.html:
<html>
<head>
<script type="text/javascript" src="test.js"></script>
</head>
<body onload=setupSvgLink()>
<object id="test-svg" type="image/svg+xml" data="test.svg" />
</body>
</html>
test.js:
function setupSvgLink() {
var svg = document.getElementById("test-svg").contentDocument;
var waldo = svg.getElementById("waldo");
waldo.setAttribute("xlink:href", "waldo.html");
}
test.svg:
<?xml version="1.0" standalone="no"?>
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="80">
<a id="waldo"><rect x="10" y="10" width="60" height="60" fill="blue"/></a>
</svg>
I am testing this with Firefox 20. I load test.html, but the rectangle does not have a link on it. I have checked in the Firefox Developer Tools' Inspector that the xlink:href attribute does appear on the element, but there is no link.
If I add the xlink:href attribute into the SVG file instead of doing it via JS it works fine.
What am I doing wrong?