Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8032475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:23:57+00:00 2026-06-05T01:23:57+00:00

So I have the following code: var element = document.getElementById(myCanvas); var width = element.width;

  • 0

So I have the following code:

var element = document.getElementById("myCanvas");
var width = element.width;                                   
var height = element.height;  
var context = element.getContext("2d");                      

/* test 1 */
var img1 = new Image(width, height);
img1.src = "http://www.mydomain.com/image.jpg";

document.body.appendChild(img1);          // <-- A: this works 
context.drawImage(img1,0,0,width,height); // <-- B: this works

/* test 2 */
var img2 = new Image(width, height);
img2.src = "http://www.notmydomain.com/image.jpg";

document.body.appendChild(img2);          // <-- C: this works 
context.drawImage(img2,0,0,width,height); // <-- D: this does not work

Okay so looking at my code, in test 1 I create an image object with a picture that is hosted on the same domain as my page. From A: I can see that it is loaded fine (A: and C: are just thrown in as tests to make sure img object loaded proper). And B: works as well, it draws the image to my canvas.

In test 2, I load an image that is hosted on a domain that is different from the my page’s domain. C: works fine, and I know you are allowed to load images hosted on other domains. However, D: does not work. I get the following error:

Error: uncaught exception: [Exception... "Security error" code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)" location: ....

Which from my understanding means this is counted as cross-site scripting.

So here are the questions:

1) Why is this considered cross-site scripting? I mean, I know why…but why is D: not allowed, when C: is? IMO they are sort of the same thing in principle/spirit?

2) Is there a way to get around this, other than traditional cross-site scripting workarounds? I think I’m going to have to use AJAX to pass the URL to a server-side script and make the request and then either save the image on the server and return a URL to it so that it is on the same domain, or else (I think) I can return the raw base64 encoded data and use the canvas methods to build it from the raw data. I can live with doing either of these things but…I’m sort of hoping maybe I’m missing something about html5/canvas stuff (I’m new to it!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:24:00+00:00Added an answer on June 5, 2026 at 1:24 am

    Hold it. Something else is going on here.

    You can absolutely draw images from a different domain.

    Here’s code at jsfiddle drawing an image from placekitten.com:

    http://jsfiddle.net/ZZW5V/

    You should try replacing the url in that fiddle code with the url of the notyourdomain image. It should still work.

    In general you should always be able to draw images onto a canvas from anywhere that you’re able to fetch them successfully from.

    What you’re not allowed to do, after that point, is get the imageData or save the canvas to a PNG with toDataUrl. There are important security reasons why that’s not allowed.

    My guess is that you’re doing something to violate one of the security rules and not just trying to draw the image.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var newDate=new Date('05/22/2012'); var month=newDate.getMonth(); var day=newDate.getDate()+(-2); var year=newDate.getYear();
I have the following code: var tlp = new TableLayoutPanel { Location = new
I have the following code: var formSubmitHandler = function (link, form) { //e.preventDefault(); var
I have the following code: var buttonOne = $(#buttonOne); var buttonTwo = $(#buttonTwo); //
I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
I have the following code: var bool:String = true; Without an if block or
I have the following code: var GoalPanelView = Backbone.View.extend({ // Bind to the goal
Following on from a previous question, I have the following code: var results =
I am using ASP.NET 3.5 with iTextSharp and I have the following code: var
Right, starting to go crazy here. I have the following code: var query =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.