Add getBaseURL

This commit is contained in:
Angelos Chalaris
2020-05-03 12:20:54 +03:00
parent ec175961d6
commit 2b94399bb4

17
snippets/getBaseURL.md Normal file
View File

@ -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'
```