JS - baseURLString when sending pathnames only
<p>I am trying to create a <code>new URL</code> in JS so it can be manipulated for an async request. As nothing is cross-origin (I think this is the correct usage of that term), the URLs I send for async request look like <code>/MyLoginUrl</code> or /<code>MyUpdateDataUrl</code>, etc. (i.e. I am only sending the pathname).</p>
<p>My attempt to create a <code>new URL</code> from an existing url looked basically like this:</p>
<pre><code>// Actually I set the url as an arguement in a function,
// but for demonstration it will be a variable
var url = '/myPathname';

// Much later...
url = new URL (url);
</code></pre>
<p>However, this was returning a syntax error. Once I looked a <a href="https://developer.mozilla.org/en-US/docs/Web/API/URL/URL" rel="nofollow noreferrer">docs</a>, I found out why.</p>
<p>Per the docs, the syntax for a <code>new URL</code> looks like this:</p>
<blockquote>
<p><code>url = new URL(urlString, [baseURLstring])</code></p>
<p><code>url = new URL(urlString, baseURLobject)</code></p>
</blockquote>
<p>The docs also say:</p>
<blockquote>
<p><code>baseURLstring</code>: is a DOMString representing the base URL to use in case urlString is a relative URL. If not specified, and no baseURLobject is passed in parameters, it default to 'about:blank'. If it is an invalid absolute URL, the constructor will raise a DOMException of type SYNTAX_ERROR</p>
</blockquote>
<p>A couple of examples in the docs for a <code>baseURLstring</code> is:</p>
<pre><code>var a = new URL(&quot;/&quot;, &quot;https://developer.mozilla.org&quot;); // Creates a URL pointing to 'https://developer.mozilla.org/'
var b = new URL(&quot;https://developer.mozilla.org&quot;);      // Creates a URL pointing to 'https://developer.mozilla.org/'
var c = new URL('en-US/docs', b);                      // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
</code></pre>
<p><strong>Thus, I am trying to figure out how to emulate a <code>baseURLstring</code> for, currently, <code>localhost</code> and eventually when this gets hosted by the main server I will use for my network, the <code>baseURLstring</code> for that. I'm guessing it would involve in some way getting the IP address of the computer I have/of the server on the network, or maybe not...</strong></p>