The createObjectURL() method creates a DOMString containing a URL representing the object given in the parameter of the method. The new object URL represents the specified File object or Blob object.
Note: The URL lifetime is tied to the document in which it was created.
Syntax:
const url = URL.createObjectURL(object);
Parameters:
- object: A File, image, or any other MediaSource object for which object URL is to be created.
Return value: A DOMString containing an object URL of that object.
Example: In this example, we will create an object URL for the image object using this method.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>URL.createObjectURL example</title> </head> <body> <h1>GeeksforGeeks</h1> <input type="file"> <img> <p class="p">The URL of this image is : </p> </body> <script> var Element = document.querySelector('input'); var img = document.querySelector('img'); Element.addEventListener('change', function() { var url = URL.createObjectURL(Element.files[0]); img.src = url; console.log(url); var d=document.querySelector(".p"); d.textContent+=url; }); </script> </html> |
Output: we will select an image from local storage and then the URL of that object will be created.
Before Choosing Image:
After Choosing Image:
Checking the created URL in a new tab:
Supported Browsers:
- Google Chrome
- Edge
- Firefox
- Safari
- Opera
Recommended Posts:
- HTML | DOM before() method
- HTML | DOM after() Method
- HTML | DOM contains() Method
- HTML | DOM insertAdjacentText() Method
- HTML DOM createHTMLDocument() Method
- HTML | DOM insertAdjacentElement() Method
- HTML DOM createDocumentType() Method
- HTML | DOM insertAdjacentHTML() Method
- HTML DOM createDocument() Method
- HTML | DOM isEqualNode() Method
- HTML | DOM getAttribute() Method
- HTML DOM matches() Method
- HTML | DOM focus() Method
- HTML | DOM removeChild() Method
- HTML | DOM setNamedItem() Method
- HTML | DOM item() Method
- HTML DOM indexedDB cmp() Method
- HTML | DOM replaceChild() Method
- HTML | DOM addEventListener() Method
- HTML | DOM getElementById() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


