title: Get site root url in javascript
body: <p>Using Joomla to create my site. Let say my site is located at</p>
  <blockquote>
  <p><a href="https://domain.com/directory/siteName" rel="nofollow noreferrer">https://domain.com/directory/siteName</a></p>
  </blockquote>
  <p>I want to get this path in javascript on any page rendered on my site. The issue is, following internal sub menus and site's different settings, the path comes to be any one like these-</p>
  <blockquote>
  <ol>
  <li><p><a href="https://domain.com/directory/siteName/anyMenu/index.php" rel="nofollow noreferrer">https://domain.com/directory/siteName/anyMenu/index.php</a></p>
  </li>
  <li><p><a href="https://domain.com/directory/siteName/index.php" rel="nofollow noreferrer">https://domain.com/directory/siteName/index.php</a></p>
  </li>
  <li><p><a href="https://domain.com/directory/siteName/index.php?option=com_content&amp;id=9" rel="nofollow noreferrer">https://domain.com/directory/siteName/index.php?option=com_content&amp;id=9</a></p>
  </li>
  <li><p><a href="https://domain.com/directory/siteName/anyMenu" rel="nofollow noreferrer">https://domain.com/directory/siteName/anyMenu</a></p>
  </li>
  <li><p><a href="https://domain.com/directory/siteName/anyMenu.html" rel="nofollow noreferrer">https://domain.com/directory/siteName/anyMenu.html</a></p>
  </li>
  </ol>
  </blockquote>
  <p>I have tried some solutions like-</p>
  <pre><code>var re = new RegExp(/^.*\//);
  alert(re.exec(window.location.href));
  </code></pre>
  <p>It truncates the last node as index.php in case 1, also</p>
  <pre><code>var dummy = new Image;
  dummy.src = '$';                 
  alert(dummy.src.slice(0,-1));
  </code></pre>
  <p>does the same thing. So is there any way to get exact address where site is located using javascript only? Also the result must be generic for any site as if it is applied in any extension then that extension can be installed in any site with any level of directory structure.</p>
  <p>I require this as I am using a js file which is internally registering another js file. So path for this, to be registered js has to be given in first js</p>