From 2b94399bb40d2b50954f68bfa725be653332bcd1 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 3 May 2020 12:20:54 +0300 Subject: [PATCH] Add getBaseURL --- snippets/getBaseURL.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 snippets/getBaseURL.md diff --git a/snippets/getBaseURL.md b/snippets/getBaseURL.md new file mode 100644 index 000000000..bda35b418 --- /dev/null +++ b/snippets/getBaseURL.md @@ -0,0 +1,17 @@ +--- +title: getBaseURL +tags: browser,string,url,beginner +--- + +Returns 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. + +```js +const getBaseURL = url => + url.indexOf('?') > 0 ? url.slice(0, url.indexOf('?')) : url; +``` + +```js +getBaseURL('http://url.com/page?name=Adam&surname=Smith'); // 'http://url.com/page' +```