Fix basehref for search box on non-root cases (#2118)

* Fix basehref for search box on non-root cases

* Use src element and correct relative path for directory return value
diff --git a/lib/resources/script.js b/lib/resources/script.js
index db4e525..152476f 100644
--- a/lib/resources/script.js
+++ b/lib/resources/script.js
@@ -240,7 +240,9 @@
   }
 
   var jsonReq = new XMLHttpRequest();
-  jsonReq.open('GET', 'index.json', true);
+  var scriptUrl = URI(document.getElementById("dartdoc_script_js").src);
+
+  jsonReq.open('GET', URI(scriptUrl.directory() + '/..').absoluteTo(scriptUrl) + '/index.json', true);
   jsonReq.addEventListener('load', function() {
     searchIndex = JSON.parse(jsonReq.responseText);
     initTypeahead();
diff --git a/lib/templates/_footer.html b/lib/templates/_footer.html
index bac8322..afcc2cc 100644
--- a/lib/templates/_footer.html
+++ b/lib/templates/_footer.html
@@ -16,7 +16,7 @@
 <script src="{{^useBaseHref}}%%__HTMLBASE_dartdoc_internal__%%{{/useBaseHref}}static-assets/typeahead.bundle.min.js"></script>
 <script src="{{^useBaseHref}}%%__HTMLBASE_dartdoc_internal__%%{{/useBaseHref}}static-assets/highlight.pack.js"></script>
 <script src="{{^useBaseHref}}%%__HTMLBASE_dartdoc_internal__%%{{/useBaseHref}}static-assets/URI.js"></script>
-<script src="{{^useBaseHref}}%%__HTMLBASE_dartdoc_internal__%%{{/useBaseHref}}static-assets/script.js"></script>
+<script id="dartdoc_script_js" src="{{^useBaseHref}}%%__HTMLBASE_dartdoc_internal__%%{{/useBaseHref}}static-assets/script.js"></script>
 <!-- footer placeholder -->
 
 </body>