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

0 votes
1,507 views
added Feb 12, 2020 in Javascript by lcjr First Warrant Officer (11,780 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, 2020 by lcjr First Warrant Officer (11,780 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.
...