Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  39] [ 4]  / answers: 1 / hits: 80292  / 14 Years ago, wed, november 10, 2010, 12:00:00

Possible Duplicate:

How do I parse a URL into hostname and path in javascript?






I’m trying to parse the url of the page. For example the current page is location.href.



So the main page of my blog, if I use alert(location.href); it will return “http://diaryofthedead.co.cc/” in an alert box. If I use alert(location.href); on page two of my blog, it will return “http://diaryofthedead.co.cc/page/2” in an alert box. Is there any way to parse the URL to get the number at the end. Does anyone know how I could do that? Could I use wildcard or something, to do something like: location.href+”page/”+*; While * is equal to whatever follows “page/”, and then turn * into a variable?


More From » url

 Answers
4

You can use



var pagenum = location.pathname.match(//page/(.*)/)[1];


It will extract anything past '/page/' in your URL;


[#95029] Saturday, November 6, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
milor

Total Points: 284
Total Questions: 93
Total Answers: 115

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;