getting the absolute path of a <img/>

Viewed 12263

Using Javascript, is there a standard way to get the absolute path of an image? img.getAttribute("src") only returns the src attribute as it was declared in the HTML.

2 Answers

For relative source path

  function getImageURI(imagePath) {
      if (imagePath.indexOf('http') == 0) {
        return imagePath
      }
      var rootPath = window.location.protocol + "//" + window.location.host + "/";
      var path = window.location.pathname;
      if (path.indexOf("/") == 0) {
          path = path.substring(1);
      }
      path = path.split("/", 1);
      if (path != "") {
          rootPath = rootPath + path + "/";
      }
      return rootPath + imagePath;
  }
Related