JAVASCRIPT
URL Utilities
All samples are using Ink.requireModules
, please read how to use it at Ink.requireModules section
Function name | Description |
---|---|
.currentScriptElement([match]) | Gets the last loaded script element |
.format(urlObj) | Formats an URL object into an URL string. |
.genQueryString(uri, params) | Generates an URL string. |
.getAnchor([str]) | Gets the URL hash value |
.getAnchorString([string]) | Gets the anchor string of an URL |
.getQueryString([str]) | Gets an object from an URL encoded string. |
.getUrl() | Gets URL of current page |
.parseUrl(url) | Parses URL string into URL parts |
Gets the last loaded script element
match
String to match against the script src attributeFind the script
element where the script is running
Code
<!-- ... -->
<div>content</div>
<script id="s">
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
var scriptElm = InkUrl.currentScriptElement();
Ink.log(scriptElm);
// <script id="s">
});
</script>
<!-- ... -->
When it is used in an external file, a string to help the match in script src
should be passed
Code
<!-- HTML file -->
<script src="https://cdn.ink.sapo.pt/3.0.0/js/ink-all.js"></script>
<!-- ... -->
<script src="other.js"></script>
<script src="file.js"></script>
<!-- ... -->
Code
// This is file.js
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
var scriptElm = InkUrl.currentScriptElement('file.js');
Ink.log(scriptElm);
// <script src="file.js">
});
Formats an URL object into an URL string.
urlObj
Window.location, a.href, or parseUrl object to formatGenerates an URL string.
uri
Base URLparams
Object to transform to query stringCode
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
var uri = InkUrl.genQueryString('https://domain.com/', {
param1:'value1',
param2:'value string'
});
Ink.log(uri);
// https://domain.com/?param1=value1¶m2=value%20string
});
Gets the URL hash value
str
URL String. Defaults to current page URL.Code
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
var hash = InkUrl.getAnchor('https://domain.com/?param=var#hashvalue');
Ink.log(hash);
// hashvalue
});
Gets the anchor string of an URL
string
URL to parse. Defaults to current URL.Code
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
// In URL: https://domain.com/#param1=value1¶m2=value2
var oHash = InkUrl.getAnchorString();
Ink.log(oHash);
// { param1: "value1", param2: "value2" }
});
A string in URL format can be passed as arguement
Code
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
var url = 'https://domain.com/#param1=value1¶m2=value2';
var oHash = InkUrl.getAnchorString(url);
Ink.log(oHash);
// { param1: "value1", param2: "value2" }
});
Gets an object from an URL encoded string.
str
URL String. When not specified it uses the current URL.Code
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
// In URL: https://domain.com/?param1=value1¶m2=value2
var oParams = InkUrl.getQueryString();
Ink.log(oParams);
// { param1: "value1", param2: "value2" }
});
A string in URL format can be passed as argument
Code
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
var url = 'https://domain.com/?param1=value1¶m2=value2';
var oParams = InkUrl.getQueryString(url);
Ink.log(oParams);
// { param1: "value1", param2: "value2" }
});
Gets URL of current page
Code
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
var url = InkUrl.getUrl();
Ink.log(url);
// https://ink.sapo.pt/javascript/Ink.Util.Url/
});
Parses URL string into URL parts
url
URL to be parsedCode
Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
var parsedURL = InkUrl.parseUrl('https://ink.sapo.pt/index.html?var1=value1#anchor');
Ink.log(parsedURL);
// {
// 'scheme' => 'http',
// 'host' => 'ink.sapo.pt',
// 'path' => '/index.html',
// 'query' => 'var1=value1',
// 'fragment' => 'anchor'
// }
});