Get Filename from URL using Javascript

Let’s say you have this URL:

and you want to get the filename, which is the jquery.js part. To do so:

const url = '';

// Returns 'jquery.js?ver=1.12.4-wp'
const filenameQuerystring = url.split('/').pop();

// Returns 'jquery.js'
const filename = filenameQuerystring.split('?').shift();

Leave a Reply

Your email address will not be published. Required fields are marked *