Even easier than using an id
is to just leave the markup alone and use document.currentScript
. The link to MDN mentions under the Notes section, a small quirk requiring the JavaScript to execute synchronously, and I'm about
to explain how to avoid this pitfall.
It's important to note that this will not reference the
element if the code in the script is being called as a callback or event handler; it will only reference the element while it's initially being processed.
The advantage of this solution is that you are not required to use special markup, and this is especially helpful if you can't access the HTML yourself for some reason [e.g. if it's used by a client or a developer using your API].
If the script is executed synchronously [meaning the main content of the script isn't wrapped in an event listener or some other function set to evaluate later than when the code is "initially being processed" ], you can simply use document.currentScript
to access the currently processing element of your code. To demonstrate how to use this effectively, I'll provide a basic demo below.
HTML:
JavaScript in //bla.com/js/script.js
:
var myScript = document.currentScript,
mySrc = myScript.getAttribute['src'];
console.log[mySrc]; // "//bla.com/js/script.js"
$[function [] {
// your other code here, assuming you use jQuery
// ...
}];
If you don't want the variable to be exposed to the global scope, you can also do this:
[function [] {
var myScript = document.currentScript,
mySrc = myScript.getAttribute['src'];
console.log[mySrc]; // "//bla.com/js/script.js"
$[function [] {
// your other code here
// ...
}];
}[]]; // self-executing anonymous function
Basically the point is to make sure and access document.currentScript
during synchronous execution of the script, otherwise it will not reference what you expect it to.
This snippet will get the filename from the url. The filename is the last part of the URL from the last trailing slash. For example, if the URL is //www.example.com/dir/file.html then file.html is the file name.
Explanation
var url = window.location.pathname;
This declares the url variable and adds the current pathname as its value.
var filename = url.substring[url.lastIndexOf['/']+1];
alert[filename];
substring [method] - extract characters from start [parameter]. url is the stringObject url.substring[start]
lastIndexOf [method] - position of last occurrence of specified string value, in this case the ‘/’
Add one to lastIndexOf because we do not want to return the ‘/’
Full snippet
var url = window.location.pathname;
var filename = url.substring[url.lastIndexOf['/']+1];
alert[filename];
The The URL lifetime is tied to the To release an object URL, call Note: This feature is available in Web Workers Note: This feature is
not available in Service Workers due to its potential to create memory leaks. A A string containing an object URL that can be used to reference the contents of the specified source URL.createObjectURL[]
static method creates a string containing a URL representing the object given in the parameter. document
in the window on which it was created. The new object URL represents the specified File
object or Blob
object. revokeObjectURL[]
.Syntax
Parameters
object
File
, Blob
, or MediaSource
object
to create an object URL for. Return value
object
.
Examples
Usage notes
Memory management
Each time you call createObjectURL[]
, a new object URL is created, even if you've already created one for the same object. Each of these must be released by calling URL.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 can explicitly 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 element required creating an object URL for the
MediaStream
. This is no longer necessary, and browsers are removing support for doing this.
Warning: If you still have code that relies on createObjectURL[]
to attach streams to media elements, you need to update your code to set
srcObject
to the MediaStream
directly.
Specifications
File API # dfn-createObjectURL |
Browser compatibility
BCD tables only load in the browser