How do i get the url of a javascript file?

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 URL.createObjectURL[] static method creates a string containing a URL representing the object given in the parameter.

The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object.

To release an object URL, call revokeObjectURL[].

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.

Syntax

Parameters

object

A File, Blob, or MediaSource object to create an object URL for.

Return value

A string containing an object URL that can be used to reference the contents of the specified source 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

Specification
File API
# dfn-createObjectURL

Browser compatibility

BCD tables only load in the browser

See also

How do I get the URL of a JavaScript page?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc. The following example will display the current url of the page on click of the button.

How do I find the URL for an uploaded file?

Answers. string UploadDir=Server. MapPath["~/UploadFolder"]; strFileName = Path. Combine[ UploadDir,FileUpload1.

What is URL in JavaScript?

URL[] The URL[] constructor returns a newly created URL object representing the URL defined by the parameters. If the given base URL or the resulting URL are not valid URLs, the JavaScript TypeError exception is thrown. Note: This feature is available in Web Workers.

How do I connect to a JavaScript file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file.

Chủ Đề