URL: createObjectURL() static method
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is available inWeb Workers, except forService Workers.
ThecreateObjectURL()
static method of theURL
interfacecreates a string containing a URL representing the object given in the parameter.
The URL lifetime is tied to thedocument
in the window on which it was created. The new object URL represents the specifiedFile
object orBlob
object.
To release an object URL, callrevokeObjectURL()
.
Note:This feature isnot available inService Workers due to itspotential to create memory leaks.
Syntax
URL.createObjectURL(object)
Parameters
object
A
File
,Blob
, orMediaSource
object tocreate an object URL for.
Return value
A string containing an object URL that can be used to reference thecontents of the specified sourceobject
.
Examples
Usage notes
Memory management
Each time you callcreateObjectURL()
, a new object URL is created, even ifyou've already created one for the same object. Each of these must be released bycallingURL.revokeObjectURL()
when you no longer need them.
Browsers will release object URLs automatically when the document is unloaded; however,for optimal performance and memory usage, if there are safe times when you canexplicitly unload them, you should do so.
Using object URLs for media streams
In older versions of the Media Source specification, attaching a stream to a<video>
element required creating an object URL for theMediaStream
. This is no longer necessary, and browsers are removingsupport for doing this.
Warning:If you still have code that relies oncreateObjectURL()
to attach streams to mediaelements, you need to update your code to setsrcObject
to theMediaStream
directly.
Specifications
Specification |
---|
File API # dfn-createObjectURL |