aaronhuisinga
aaronhuisinga

Reputation: 299

Get Specific Piece of Page URL with jQuery and REGEX

I'm stuck trying to figure out the best way to do this. Currently, I have a function that pulls just the filename from the page URL.

For example. http://www.example.com/example1/thepage.php?page=1/ will return thepage.php?page=1.

How can I tell the function to only return 'thepage.php', and stop at the first question mark?

The current code:

var pageName = location.href.substr(location.href.lastIndexOf("/")+1,location.href.length);

Thanks for the help!

Upvotes: 1

Views: 113

Answers (1)

andyb
andyb

Reputation: 43823

You could just use the already existing JavaScript window.location.pathname which will return /thepage.php for the example in the question. I guess you just need to strip the first slash off.

See window.location on MDN for a comprehensive guide to that JavaScript object.

Upvotes: 3

Related Questions