Are you first timer at lazacode.org? Why not join us and start exchange your programming knowledge with everyone. Register today!

JS - Get specific slash value on URL with split()

0 votes
95 views
added Feb 12 in Javascript by lcjr Second Warrant Officer (3,240 points)
var newsUrl = window.location.href,
    newsSplit = newsUrl.split('/'),
    newsId = newsSplit[newsSplit.length-2]; 
console.log(newsId);

// e.g URL:
// http://yoursite.com/content/1643743/defender-110-td5-2006-England

// output newsId
// 1643743

 

1 Response

0 votes
responded May 13 by lcjr Second Warrant Officer (3,240 points)
function currentID() {
  var current_path = window.location.pathname;
  var path_arr = current_path.split("/");
  return path_arr[2];
}

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...