SipaUrl
SipaUrl
SipaUrl
Tool class to access and manipulate the current or given URLs
- SipaUrl
- .getUrl() →
string - .getProtocol() →
'http'|'https' - .getHostName() →
string - .getParams() →
Object.<string, string> - .setParams(params)
- .setParam(param_key, value)
- .removeParams(param_keys)
- .removeParam(param_key)
- .setAnchor(anchor, jump)
- .removeAnchor()
- .getAnchor() →
string - .createUrlParams(params, options) →
string - .getParamsOfUrl(url, options) →
Object.<string, string> - .removeParamsOfUrl(url, param_keys) →
string - .removeParamOfUrl(url, param_key)
- .setParamsOfUrl(url, params) →
string - .setAnchorOfUrl(url, anchor) →
string - .getAnchorOfUrl(url, options) →
string - .removeAnchorOfUrl(url) →
string
- .getUrl() →
SipaUrl.getUrl() → string
Get the current address of the website
Example
SipaUrl.getUrl();
// => https://my-website.com/web/?page=abc¶m=ok
SipaUrl.getProtocol() → 'http' | 'https'
Get the protocol of the current url (without colon)
Example
SipaUrl.getProtocol();
// => 'https'
SipaUrl.getHostName() → string
Get the host name of the current url
Example
localhost
127.0.0.1
localhost:7000
my-domain.com
SipaUrl.getParams() → Object.<string, string>
Get all params of the current URL
Example
// URL: https://my-business.com/?one=1&stat=true
SipaUrl.getParams();
// => { "one": "1", "stat": "true" }
SipaUrl.setParams(params)
Set or overwrite given parameters of the current url
| Param | Type | Description |
|---|---|---|
| params | Object.<string, string> | in format { param1: value1, param2: value2, ... } |
Example
// URL: https://my-business.com/?one=1&stat=true&that=cool
SipaUrl.setParams({ "more": "better", "stat": "false"});
// URL: https://my-business.com/?one=1&stat=false&that=cool&more=better
SipaUrl.setParam(param_key, value)
Set or overwrite one specific parameter of the current url
| Param | Type |
|---|---|
| param_key | string |
| value | string |
Example
// URL: https://my-business.com/?super=banana&coca=cola
SipaUrl.setParam("pepsi","coke");
// URL: https://my-business.com/?super=banana&coca=cola&pepsi=coke
SipaUrl.removeParams(param_keys)
Remove given params of the current url
| Param | Type |
|---|---|
| param_keys | Array.<String> |
Example
// URL: https://my-business.com/?some=stuff&foo=bar&more=power
SipaUrl.removeParams(["some","more"]);
// URL: https://my-business.com/?foo=bar
SipaUrl.removeParam(param_key)
Remove given param of the current url
| Param | Type |
|---|---|
| param_key | string |
Example
// URL: https://my-business.com/?some=stuff&foo=bar
SipaUrl.removeParam("foo");
// URL: https://my-business.com/?some=stuff
SipaUrl.setAnchor(anchor, jump)
Set or overwrite given anchor of the current url
| Param | Type | Default | Description |
|---|---|---|---|
| anchor | string | without leading # character | |
| jump | boolean | false | jump to anchor |
SipaUrl.removeAnchor()
Remove the anchor of the current URL
Example
// URL: https://my-business.com/?some=stuff&foo=bar#my-anchor
SipaUrl.removeAnchor();
// URL: https://my-business.com/?some=stuff&foo=bar
SipaUrl.getAnchor() → string
Get the anchor of the current URL without leading #
SipaUrl.createUrlParams(params, options) → string
Creates an url query string based on the given key<->value object
| Param | Type | Description |
|---|---|---|
| params | Object.<string, string> | in format { param1: value1, param2: value2, ... } |
| options | Object | |
| options.url_encode | boolean | url encode parameter keys and values, default: true |
| options.multi_param_attributes | boolean | if attribute is of array, make it 'id=1&id=2&id=3' on true, or 'id=1,2,3' on false |
Example
{ a: 1, b: [1,2,3], c: "test space" }
=>
'a=1&b=1&b=2&b=3&c=test%20space'
SipaUrl.getParamsOfUrl(url, options) → Object.<string, string>
Create a JSON, containing the parameters of the given url
Returns: Object.<string, string> - return a JSON with { param1: value1, param2: value2, ... }
| Param | Type | Description |
|---|---|---|
| url | string | the url to extract parameters from |
| options | Object | |
| options.decode_uri | boolean | decode uri parameter values |
Example
SipaUrl.getParamsOfUrl("https://my-business.com/?some=stuff&foo=bar");
// => { "some": "stuff", "foo": "bar" }
SipaUrl.removeParamsOfUrl(url, param_keys) → string
Remove the given parameters from the given url
| Param | Type | Description |
|---|---|---|
| url | string | to remove the params from |
| param_keys | Array.<String> | array of keys to remove from the given url, e.g. ['key1','key2'} |
SipaUrl.removeParamOfUrl(url, param_key)
Remove the given one parameter from the given url
| Param | Type | Description |
|---|---|---|
| url | string | |
| param_key | string | name of the param |
SipaUrl.setParamsOfUrl(url, params) → string
Set/overwrite the parameters of the given url
Returns: string - with given parameters
| Param | Type | Description |
|---|---|---|
| url | string | |
| params | Object.<string, string> | in format { param1: value1, param2: value2, ... } |
SipaUrl.setAnchorOfUrl(url, anchor) → string
Set/overwrite the anchor of the given url
Returns: string - with given anchor
| Param | Type | Description |
|---|---|---|
| url | string | |
| anchor | string | as string, without leading # |
SipaUrl.getAnchorOfUrl(url, options) → string
Get the anchor of the given url
Returns: string - the anchor of the given url
| Param | Type | Description |
|---|---|---|
| url | string | |
| options | object | |
| options.return_prefixed_hash | boolean | return the prefixed hash |
SipaUrl.removeAnchorOfUrl(url) → string
Remove the anchor of the given url
Returns: string - without anchor
| Param | Type |
|---|---|
| url | string |