<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Waqas Naeem Bajwa</title>
    <description>The latest articles on DEV Community by Waqas Naeem Bajwa (@devbajwa).</description>
    <link>https://dev.to/devbajwa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F917932%2F92238b05-8292-4f93-a4d8-b51e4335a40c.jpeg</url>
      <title>DEV Community: Waqas Naeem Bajwa</title>
      <link>https://dev.to/devbajwa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devbajwa"/>
    <language>en</language>
    <item>
      <title>How to find pathname of any URL - Javascript</title>
      <dc:creator>Waqas Naeem Bajwa</dc:creator>
      <pubDate>Tue, 30 Aug 2022 12:07:28 +0000</pubDate>
      <link>https://dev.to/devbajwa/how-to-find-pathname-of-any-url-javascript-2bcl</link>
      <guid>https://dev.to/devbajwa/how-to-find-pathname-of-any-url-javascript-2bcl</guid>
      <description>&lt;p&gt;In this article I will share the knowledge of converting a &lt;code&gt;string&lt;/code&gt; into &lt;code&gt;URL&lt;/code&gt; and manipulating the properties of Uniform Resource Locator (URL).&lt;/p&gt;

&lt;p&gt;Let's start 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 1
&lt;/h2&gt;

&lt;p&gt;We need current &lt;code&gt;pathname&lt;/code&gt; from URL of the current webpage to consume it in our application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;We can get URL of the current webpage with window object. The following code gives us the &lt;code&gt;location&lt;/code&gt; object of the current document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(window.location);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;window.location&lt;/code&gt; will return the following Location object as a read-only result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  hash: ""
  host: "https://devbajwa.com"
  hostname: "https://devbajwa.com"
  href: "https://devbajwa.com/programming/languages/javascript/objects"
  origin: "https://devbajwa.com"
  pathname: "/programming/languages/javascript/objects"
  port: ""
  protocol: "https:"
  search: ""
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;window.location&lt;/code&gt; object has several properties which carries the information about the location of the current document.&lt;/p&gt;

&lt;p&gt;Now, suppose the following URL is current document &lt;code&gt;https://devbajwa.com/programming/languages/javascript/objects&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  href
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;window.location.href&lt;/code&gt; gives us the &lt;strong&gt;absolute URL&lt;/strong&gt; of the current document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(window.location.href)
-&amp;gt;
"https://devbajwa.com/programming/languages/javascript/objects"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  pathname
&lt;/h4&gt;

&lt;p&gt;Similarly we can access the &lt;code&gt;pathname&lt;/code&gt; with &lt;code&gt;window.location.pathname&lt;/code&gt; property. This is also referred as &lt;strong&gt;relative URL&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(window.location.pathname)
-&amp;gt;
"/programming/languages/javascript/objects"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Scenario 2
&lt;/h2&gt;

&lt;p&gt;We &lt;code&gt;fetch&lt;/code&gt; JSON data from an API call and receive a response with an absolute URL. We need &lt;code&gt;pathname&lt;/code&gt; of that absolute URL. &lt;/p&gt;

&lt;p&gt;Since the response of an API call is in &lt;code&gt;string&lt;/code&gt; format and not from the &lt;code&gt;window.location&lt;/code&gt; object, we don't have access the &lt;code&gt;location&lt;/code&gt; or &lt;code&gt;pathname&lt;/code&gt; property for that &lt;code&gt;string&lt;/code&gt; value(URL).&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const absoluteURL = "https://devbajwa.com/programming/languages/javascript/objects";

console.log(absoluteURL.location);
-&amp;gt;
undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we try to access &lt;code&gt;location&lt;/code&gt; object of our custom &lt;code&gt;string&lt;/code&gt; format URL, we will get &lt;code&gt;undefined&lt;/code&gt; response.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;We can leverage modern &lt;strong&gt;URL API&lt;/strong&gt; available that accesses and manipulates URLs. The &lt;code&gt;new URL(string)&lt;/code&gt; parses the &lt;code&gt;string&lt;/code&gt; format URL and provides access to the integral parts through its properties.&lt;/p&gt;

&lt;p&gt;Simply pass the &lt;code&gt;string&lt;/code&gt; formatted URL to the &lt;code&gt;new URL()&lt;/code&gt; function and the resulted value is a &lt;code&gt;URL&lt;/code&gt; now. We have access to various properties.&lt;/p&gt;

&lt;h4&gt;
  
  
  href
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const absoluteURL = new URL("https://devbajwa.com/programming/languages/javascript/objects");

console.log(absoluteURL.href);
-&amp;gt;
"https://devbajwa.com/programming/languages/javascript/objects"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  pathname
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const absoluteURL = new URL("https://devbajwa.com/programming/languages/javascript/objects");

console.log(absoluteURL.pathname);
-&amp;gt;
"/programming/languages/javascript/objects"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Codepen
&lt;/h3&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/devbajwa/embed/PoRMmoW?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
 &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Modern Javascript is becoming more declarative now a days. We should leverage the available modern Web APIs to make our code more readable and developer friendly. However, keep &lt;a href="https://caniuse.com/?search=URL()"&gt;check on the browser support&lt;/a&gt; before use :) &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>api</category>
      <category>url</category>
      <category>pathname</category>
    </item>
  </channel>
</rss>
