getImageURL

Category:UI Controls

Gets the URL for the provided image element id.

The images in apps are not always static, they sometimes change based on user or other events. getImageURL() can be used to access the URL of the image displayed for an image element ID.

Examples

// Display the URL of the Code.org logo.
image("logo", "http://code.org/images/logo.png");
write(getImageURL("logo"));

Example: Image Swap

Click the button to change the character image from the dog to the bee and back again.

// Click the button to change the character image from the dog to the bee and back again.
var imageId = "character";
var dogImageURL = "http://studio.code.org/blockly/media/skins/applab/static_avatar.png";
var beeImageURL = "http://studio.code.org/blockly/media/skins/bee/static_avatar.png";
image(imageId, dogImageURL);
button("changeButton", "Change Character");
onEvent("changeButton", "click", function() {
  if (getImageURL(imageId).includes("bee")) {
    setImageURL(imageId, dogImageURL);
  } else {
    setImageURL(imageId, beeImageURL);
  }
});

Syntax

getImageURL(id);

Parameters

Name Type Required? Description
id string The unique identifier for the screen element. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _.

Returns

Returns a string containing image url for the provided id.

Tips

  • The string returned contains the image URL. For example http://code.org/images/logo.png is returned as https://studio.code.org/media?u=http%3A%2F%2Fcode.org%2Fimages%2Flogo.png

Found a bug in the documentation? Let us know at documentation@code.org