get file url in javascript code example

Example 1: get filename from url js

var filename = location.pathname.substr(location.pathname.lastIndexOf("/")+1);

Example 2: javascript get filename from url

var filename = url.split('/').pop();

Example 3: javascript read file from url

// read text from URL location
function getText(){
    var request = new XMLHttpRequest();
    request.open('GET', 'http://www.xxxxx.com/pocket.txt', true);
    request.send(null);
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            var type = request.getResponseHeader('Content-Type');
            if (type.indexOf("text") !== 1) {
                return request.responseText;
            }
        }
    }
}