{"id":6541,"date":"2021-09-22T11:27:44","date_gmt":"2021-09-22T09:27:44","guid":{"rendered":"http:\/\/blog.bart.sk\/en\/?p=6541"},"modified":"2024-01-29T10:30:19","modified_gmt":"2024-01-29T09:30:19","slug":"3-web-apis-may-not-know","status":"publish","type":"post","link":"https:\/\/blog.bart.sk\/en\/3-web-apis-may-not-know\/","title":{"rendered":"3 Web APIs that you may (not) know"},"content":{"rendered":"\n<p><span style=\"font-weight: 400;\">In today&#8217;s article, we will introduce 3 Web APIs that, when used correctly, can make the job easier for many web developers.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">URL&nbsp;API<\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">As the name suggests, the <\/span><b>URL API is used to work with URL addresses.<\/b><span style=\"font-weight: 400;\"> URL consists of several parts: protocol, hostname, port, path and query parameters.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2021\/09\/urldiagm.png\"><img decoding=\"async\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2021\/09\/urldiagm.png\" alt=\"\" class=\"wp-image-8015\"\/><\/a><\/figure><\/div>\n\n\n<p><span style=\"font-weight: 400;\">With this API we can not only find out the individual parts, but we can also easily modify them.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">At the beginning we create a new object of URL type, where we put the URL address we want to work with as input parameter.<\/span><\/p>\n\n\n\n<p><b>For example:<\/b><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">var<\/span><span style=\"font-weight: 400;\"> url <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">new<\/span><span style=\"font-weight: 400;\"> URL<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"https:\/\/www.nejaka-stranka.test\/blog\/clanky?datum=2021-06-22&amp;stranka=3\"<\/span><span style=\"font-weight: 400;\">);<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">If we want to get the GET value of the <\/span><b>date<\/b><span style=\"font-weight: 400;\"> parameter, we simply do it via:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">url<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">searchParams<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">get<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"datum\"<\/span><span style=\"font-weight: 400;\">));<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">We can list all GET parameters by iterating the value of <\/span><b>searchParams<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">for<\/span> <span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">let<\/span><span style=\"font-weight: 400;\"> p <\/span><span style=\"font-weight: 400;\">of<\/span><span style=\"font-weight: 400;\"> url<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">searchParams<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{<\/span>\n<span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">p<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">We will use the <\/span><b>set method to modify the parameter<\/b><span style=\"font-weight: 400;\">.<\/span> <span style=\"font-weight: 400;\">In addition to modifying an existing parameter, we can use the same method to add a new parameter.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">url<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">searchParams<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">set<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"datum\"<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\"2020-09-28\"<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">url<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">searchParams<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">set<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"novyParameter\"<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\"35\"<\/span><span style=\"font-weight: 400;\">);<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">When we subsequently want to get the whole URL back as a text string, we call the toString() method:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">url<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">toString<\/span><span style=\"font-weight: 400;\">());<\/span><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><b>History API<\/b><\/h2>\n\n\n\n<p class=\"has-text-align-left\"><span style=\"font-weight: 400;\">We can use the History API to manipulate the history of the browser. For example, we can replace the Back and Next buttons. We will use the <\/span><b>back()<\/b><span style=\"font-weight: 400;\"> method to go back 1 page:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">history.back();<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Similarly, if we want to go one page forward in the history, we use the <\/span><b>forward()<\/b><span style=\"font-weight: 400;\"> method:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">history<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forward<\/span><span style=\"font-weight: 400;\">();<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">If we want to move several pages in the history, we use the <\/span><b>go()<\/b><span style=\"font-weight: 400;\"> method. This method has the number where we want to move in the history as input parameter. For example, the number <\/span><b>-3<\/b><span style=\"font-weight: 400;\"> means that we want <\/span><b>to move 3 pages back<\/b><span style=\"font-weight: 400;\">. If we use 0 as the parameter, or omit the parameter, the current page will be refreshed.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">history<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">go<\/span><span style=\"font-weight: 400;\">(-<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">);<\/span><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2021\/09\/42387601.png\"><img decoding=\"async\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2021\/09\/42387601.png\" alt=\"\" class=\"wp-image-8003\"\/><\/a><\/figure><\/div>\n\n\n<p><span style=\"font-weight: 400;\">HTML5 has introduced 2 new methods to History API: <\/span><b>pushState()<\/b><span style=\"font-weight: 400;\"> and <\/span><b>replaceState()<\/b><span style=\"font-weight: 400;\">. These methods modify the history without refreshing the page. They&#8217;re very useful for single-page applications that consist of different subpages. We can move between subpages without refreshing the page. The History API is also used in frameworks such as Nuxt.js or React Router.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">History in single-page applications behaves like a stack. The <\/span><b>pushState()<\/b><span style=\"font-weight: 400;\"> method inserts a new record at the top of the stack. The <\/span><b>replaceState() <\/b><span style=\"font-weight: 400;\">method modifies the record we are currently on. When we go back a few pages in the history and then call <\/span><b>pushState()<\/b><span style=\"font-weight: 400;\">, the entire history of the following pages is replaced by one new record.<\/span><\/p>\n\n\n\n<p><b>Both methods have 3 input parameters:<\/b><\/p>\n\n\n\n<ul>\n<li><span style=\"font-weight: 400;\">The first parameter is the data that we want to save to the subpage (record). The data can be, for example, a text string or an object. We can then obtain this data in <\/span><b>an onpopstate <\/b><span style=\"font-weight: 400;\">event via <\/span><b>event.state<\/b><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">The second parameter is the page title. This parameter is currently ignored on all browsers except Safari. But that may change in the future.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">The third parameter is the URL address. It can be relative or absolute. However, the absolute address must contain the hostname where we are actually located. For security reasons, the browser will not allow us to change the URL address to a completely foreign page.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>For example, if we want to move to a new subpage, we call:<\/b><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">history<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">pushState<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"nejake d\u00e1ta\"<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\"titulok\"<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\"\/relativna-adresa\/novej\/podstranky\"<\/span><span style=\"font-weight: 400;\">);<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">But when do we use the replaceState method? A typical use case is when by using the pushState method we would get into a loop in history. It must have happened to y<\/span><span style=\"font-weight: 400;\">ou \u2013 y<\/span><span style=\"font-weight: 400;\">ou wanted to return to a previous page from some page, but it wasn\u2019t possible with the &#8216;back&#8217; button, because history kept repeating itself.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">For example, when we want to move to the \/admin subpage, which, however, requires a login, so the subpage redirects us to \/login. If we do the re-routing through pushState, clicking on the &#8216;back&#8217; button would take us back to the \/admin subpage and it would move us back to \/login and so on. However, when we do the re-routing from \/admin to \/login via <\/span><b>the replaceState()<\/b><span style=\"font-weight: 400;\"> method, after clicking the back button, we don&#8217;t return to \/admin, but to the subpage we came from.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The methods themselves would not be very useful if we couldn&#8217;t find out that the subpage had changed. And so, in addition to these methods, a new event was added: <\/span><b>onpopstate<\/b><span style=\"font-weight: 400;\">. This event is called whenever we move forward or backward in history.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">window<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">onpopstate <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">event<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{\n<\/span><span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"podstranka: \"<\/span> <span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\"> document<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">location<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">href<\/span><span style=\"font-weight: 400;\">);\n<\/span><span style=\"font-weight: 400;\">}<\/span><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Geolocation API<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2021\/09\/pasted-image-0.png\"><img decoding=\"async\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2021\/09\/pasted-image-0.png\" alt=\"\" class=\"wp-image-8004\"\/><\/a><\/figure><\/div>\n\n\n<p><span style=\"font-weight: 400;\">The Geolocation API is used to obtain the GPS coordinates you are currently on. We use this API, for example, on the drmax project to find pharmacies in a customer&#8217;s vicinity on the subpage <\/span><a href=\"https:\/\/www.drmax.sk\/lekarne\"><span style=\"font-weight: 400;\">https:\/\/www.drmax.sk\/lekarne<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">For security reasons, access to the geolocation API is only possible after access is granted. Therefore, before obtaining the coordinates, the browser asks us if we want to grant the site rights to access the geolocation API. Only after approval can we access the aforementioned API.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The <\/span><b>getCurrentPosition()<\/b><span style=\"font-weight: 400;\"> method is used to get the coordinates. As an input parameter, this method has a callback to a function that gets a <\/span><b>GeolocationPosition <\/b><span style=\"font-weight: 400;\">object as an input parameter. We can list the coordinates by calling:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> showPosition<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">position<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{<\/span>\n<span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">position<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">coords<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n<span style=\"font-weight: 400;\">if<\/span> <span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">navigator<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">geolocation<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{<\/span>\n<span style=\"font-weight: 400;\">navigator<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">geolocation<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">getCurrentPosition<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">showPosition<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">However, there may be a situation in the application where obtaining coordinates once won&#8217;t be enough, but we&#8217;ll want to monitor every change of position (for example, an application for a taxi service). We can use the <b>watchPosition()<\/b> method for this purpose. <\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">var<\/span><span style=\"font-weight: 400;\"> watchID <\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\"> navigator<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">geolocation<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">watchPosition<\/span><span style=\"font-weight: 400;\">((<\/span><span style=\"font-weight: 400;\">position<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">=&gt;<\/span> <span style=\"font-weight: 400;\">{<\/span>\n<span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">position<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">coords<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">latitude<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> position<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">coords<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">longitude<\/span><span style=\"font-weight: 400;\">);<\/span> \n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">The <\/span><b>watchPosition()<\/b><span style=\"font-weight: 400;\"> method returns the ID for the watcher. When we no longer need to track a user&#8217;s location, we can cancel the tracking using the returned ID and the <\/span><b>clearWatch()<\/b><span style=\"font-weight: 400;\"> method:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">navigator<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">geolocation<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">clearWatch<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">watchID<\/span><span style=\"font-weight: 400;\">);<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">&nbsp;<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Conclusion<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">That would be all for today&#8217;s article. Next time we&#8217;ll introduce you to other useful Web APIs.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"In today&#8217;s article, we will introduce 3 Web APIs that, when used correctly, can make the job easier&hellip;","protected":false},"author":22,"featured_media":6579,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","csco_display_header_overlay":false,"csco_singular_sidebar":"","csco_page_header_type":""},"categories":[199,211],"tags":[226],"_links":{"self":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/6541"}],"collection":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/comments?post=6541"}],"version-history":[{"count":10,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/6541\/revisions"}],"predecessor-version":[{"id":7375,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/6541\/revisions\/7375"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/media\/6579"}],"wp:attachment":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/media?parent=6541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/categories?post=6541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/tags?post=6541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}