Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  130] [ 3]  / answers: 1 / hits: 20749  / 14 Years ago, wed, january 19, 2011, 12:00:00

In a jQuery script I have the line of code which gets the string of current URL:



var target = $(this).attr('href');


Could the code in this script check if there is a slash at the end of the URL string. If it is present then remove it? What is the way to do it, you could recommend?


More From » jquery

 Answers
19

I'd do this:



 target = target.replace(//$/, '');


Now if you need to worry about the presence of a query string:



 <a href='http://foo.bar.com/something/?param=xyz'>hi</a>


then things get a little more tricky. Parsing a URL with a regex is probably possible, but it's pretty messy. If you can get away with it, narrow down the possibilities of what your URLs can look like so that you don't have to use some big huge official pattern.


[#94140] Tuesday, January 18, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaredsages

Total Points: 273
Total Questions: 97
Total Answers: 105

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
jaredsages questions
;