Categories
Javascript

Get Filename from URL using Javascript

Let’s say you have this URL:

http://wp.com/wp-includes/js/jquery/jquery.js?ver=1.12.4-wp

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

const url = 'http://wp.com/wp-includes/js/jquery/jquery.js?ver=1.12.4-wp';

// 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 *