All samples are using Ink.requireModules, please read how to use it at Ink.requireModules section

Ink.Util.Url_1 class

Functions
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

.currentScriptElement([match]) method

Gets the last loaded script element

Accepts

  • match

    String to match against the script src attribute

Find 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">
});

.format(urlObj) method

Formats an URL object into an URL string.

Accepts

  • urlObj

    Window.location, a.href, or parseUrl object to format

.genQueryString(uri, params) method

Generates an URL string.

Accepts

  • uri

    Base URL
  • params

    Object to transform to query string

Code

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&param2=value%20string 
});

.getAnchor([str]) method

Gets the URL hash value

Accepts

  • 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 
});

.getAnchorString([string]) method

Gets the anchor string of an URL

Accepts

  • string

    URL to parse. Defaults to current URL.

Code

Ink.requireModules(['Ink.Util.Url_1'], function(InkUrl) {
             
    // In URL: https://domain.com/#param1=value1&param2=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&param2=value2'; 
    var oHash = InkUrl.getAnchorString(url); 
    Ink.log(oHash);
    // { param1: "value1", param2: "value2" }    
});

.getQueryString([str]) method

Gets an object from an URL encoded string.

Accepts

  • 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&param2=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&param2=value2';
    var oParams = InkUrl.getQueryString(url); 
    Ink.log(oParams);
    // { param1: "value1", param2: "value2" }    
});

.getUrl() method

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/
});

.parseUrl(url) method

Parses URL string into URL parts

Accepts

  • url

    URL to be parsed

Code

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'
    // }
});