Search code examples
javascripthtmlgoogle-chromesvgpanzoom

Error on chrome when trying to access svg in object


I don't have much experience with coding and I'm trying to give a user some control over an SVG embedded in an element. I found the ariutta svgpanzoom.js library, but when I try to make a small test with it, I have a security error in chrome (everything works well with Firefox and Safari)

Here's my basic code

  
<!DOCTYPE html>
<html>
<head>
	<title>SVG test</title>
	<meta charset="utf-8">
	<script src="http://ariutta.github.io/svg-pan-zoom/dist/svg-pan-zoom.js"></script>
</head>
<body>
	<p><h1>Test SVG in object tag</h1></p>
	<br>
	<object id="mySVG" type="image/svg+xml" data="../Tests/simpleSVG.svg" width="400" height="400" style="border: 1px solid red;"></object>
	
	<script>
      window.onload = function() {
        svgPanZoom("#mySVG", {
          zoomEnabled: true,
          controlIconsEnabled: true
        });
      };
    </script>

</body>
</html>

The error I get on Chrome is: Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLObjectElement': Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

Does anyone have an idea of what I did wrong ?

Thanks in advance...


Solution

  • Bumbu is right : Chrome blocks the access to local documents (Object in this case) from remote scripts (svg-pan-zoom).

    The code works well when run from a local server.