Kei Izumi Kei Izumi -5 years ago 198
jQuery Question

How to extract the filename of URL in JavaScript?

I'm trying to extract the current file name in Javascript without any parameters.

var current_path = RegExp.$1;
if ((current_path == 'index.html') || ...) {
// something here

But it doesn't work at all when you access like Sure before the file name will be changed at random.

Any idea?

Answer Source

If you're looking for the last item in the path, try this:

var current_path = window.location.pathname.split('/').pop();



will give you something like:


Then the .split() will split the string into an Array, and .pop() will give you the last item in the Array.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download