Vivek Vikky
Vivek Vikky

Reputation: 15

JavaScript runtime error: Unspecified error

My requirement is to return data URL. But, when I run the application, there is an run-time error:

JavaScript runtime error: Unspecified error.

Here is the code that I have used. Temp path is the path where is location of image is.

var canvas = document.createElement("canvas");   
var context = canvas.getContext("2d");         
var img = new Image();         
img.src = "@tempPath";                 
context.drawImage(img, 40, 40);         
var dataURL = canvas.toDataURL("image/jpeg");         
alert(dataURL);'

Upvotes: 0

Views: 1716

Answers (1)

VikrantMore
VikrantMore

Reputation: 903

Try following code, It worked for me:

<body>
    <canvas id="myCanvas"></canvas>
    <img id="profileImg" alt=""/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            drawImg();
        });
        function drawImg() {
            try {
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
                var img = new Image();
                img.src = $('#profileImg');
                context.drawImage(img, 40, 40);
                var dataURL = canvas.toDataURL("image/jpeg");
                alert(dataURL);
            } catch (e) {
                if (e.name == "NS_ERROR_NOT_AVAILABLE") {
                    // This is a bug in Firefox. The easiest fix is to simply keep trying until the error goes away, 
                    //since no event fires at the correct time.
                    // Wait before trying again; you can change the length of this delay.
                    setTimeout(drawImg, 100);
                } else {
                    throw e;
                }
            }
        }
    </script>
</body> 

Works well with IE as well. Hope this helps.

Upvotes: 2

Related Questions