Files
30-seconds-of-code/snippets/getBaseURL.md
Isabelle Viktoria Maciohsek aedcded750 Format snippets
2020-10-22 20:23:47 +03:00

433 B

title, tags
title tags
getBaseURL string,browser,beginner

Gets the current URL without any parameters.

  • Use String.prototype.indexOf() to check if the given url has parameters, String.prototype.slice() to remove them if necessary.
const getBaseURL = url =>
  url.indexOf('?') > 0 ? url.slice(0, url.indexOf('?')) : url;
getBaseURL('http://url.com/page?name=Adam&surname=Smith');
// 'http://url.com/page'