0

Can someone help me understand why chrome might raise a security exception when I load this page from the local filesystem? Opera and Firefox don't have a problem and from what I've read the origin-clean flag should still be true.

<html>
<head>
<title>Canvas Test</title>
<script type="text/javascript">

    var canvas = document.createElement("canvas");
    canvas.setAttribute("width", 100);
    canvas.setAttribute("height", 100);

    var myImage = new Image();
    myImage.src = "gradient-1.png";

    function draw(){  
        document.body.appendChild(canvas);
        var ctx = canvas.getContext("2d");  
        ctx.drawImage(myImage, 0, 0, 100, 100);
        data = canvas.toDataURL("image/png");
        // Chrome says Uncaught Error: SECURITY_ERR: Dom Exception 18
    }

</script>
</head>

<body onload="draw()">
</body>
</html>
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • 1
    See [this question](http://stackoverflow.com/questions/4270999/google-chrome-allow-file-access-from-files-disabled-for-chrome-beta-8). – jholster Feb 23 '12 at 09:32
  • Thanks @jholster, that was informative. I'm mostly concerned about a phonegap android app I'm working on. Do you know if the same issue applies? – pguardiario Feb 23 '12 at 09:49
  • Sorry, no idea, but one could assume it's a feature of Chrome, not webkit. – jholster Feb 23 '12 at 13:01
  • @pguardiario - Do you have info on the same , am running into same issue – surya May 30 '17 at 11:41
  • You aren't running it on a server? It's just a client side .html file? If that's the case, there are several criteria to be met to satisfy the CORS Policy. My guess, I could be completely wrong, is that you are not satisfying the port requirement, seeing that its not a server listening to a specific port... Correct me if I am wrong...or if you have the solution seeing its been 8 years... – Shmack Oct 25 '20 at 01:48

1 Answers1

0

My best guess would be you are loading the html file directly from the file system in your browser. I saw the same issue when i started playing with with canvas and not running my code through my local web server. Give that a try, otherwise it's most likely a corrupt file because your code is correct.

nomis
  • 1,582
  • 14
  • 14