-1

I have a simple HTML page that is attempting to access both remote and local JavaScript files:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="https://js.arcgis.com/3.19/"></script>
    <script src="SearchExtent.js"></script>
    <script>
        require([
            ...

SearchExtent.js is stored in the same folder as the HTML page.

When debugging the HTML page both in Chrome and Edge, a 404 error occurs indicating that the following resource can't be found:

https://js.arcgis.com/3.19/SearchExtent.js

Why is the browser looking on the remote service instead of the local filesystem>

Jonathan Bailey
  • 306
  • 2
  • 13
  • 2
    Try using a forward slash? – MrVentzi Jan 23 '17 at 15:33
  • Possible duplicate of [What is the right way to write my script 'src' url for a local development environment?](http://stackoverflow.com/questions/16677095/what-is-the-right-way-to-write-my-script-src-url-for-a-local-development-envir) – Nope Jan 23 '17 at 15:33
  • @Pointy I believe he means in front of the `SearchExtent.js` so it would look like `` – Quiver Jan 23 '17 at 15:34
  • 1
    What is ` – Pointy Jan 23 '17 at 15:34
  • 1
    @Quiver ok, maybe, but there's nothing wrong with that ` – Pointy Jan 23 '17 at 15:35
  • Have you checked that you uploaded the SearchExtent.js to the server? Also it is good practice to keep your .js files in separate folder (ie. /js/SearchExtent.js). – Frederick M. Rogers Jan 23 '17 at 15:37
  • This question might be helpful: http://stackoverflow.com/questions/20748630/load-local-javascript-file-in-chrome-for-testing Please check out the --allow-file-access-from-files option for chrome. – Eduard Malakhov Jan 23 '17 at 15:38
  • @EduardMalakhov that's not relevant; it only applies to XHR local file loading. – Pointy Jan 23 '17 at 15:39
  • @Fran The suggested duplicate yields the same result. – Jonathan Bailey Jan 23 '17 at 15:48
  • And, to the downvoter: It's a terrific question. Everybody knows it. I did research. I did a tremendous amount of research. It was fabulous research. Believe me! – Jonathan Bailey Jan 23 '17 at 15:56
  • @SpatialBridge The error you are seeing might not be because of the script tag. The main question is have you used the "SearchExtent" in you require block as well? – T Kambi Jan 23 '17 at 15:58
  • @TKambi Yes, SearchExtent is in the require block. However, the error indicates that the browser is looking for SearchExtent as js.arcgis.com. – Jonathan Bailey Jan 23 '17 at 16:00
  • @SpatialBridge if you add the `require(["SearchExtent", ..], function(SearchExtent, ..){..}` the `require` will try to download the file from the esri location as that how it is configured in dojo config. You may want to check out how to add custom modules using dojo. – T Kambi Jan 23 '17 at 16:04
  • @TKambi OK. I looked at the dojotoolkit documentation on configuring dojo. I modified the code as indicated to define custom package location, but now the browser is looking for SearchExtent.js on the Google CDN. If I place the reference to the esri scripts after the dojo configuration, the browser is trying to get the esri scripts from the Google CDN. Also, I know that the esri scripts reference dojo. – Jonathan Bailey Jan 23 '17 at 16:20
  • @SpatialBridge Please share the configuration you have made. without that I am unable to understand what changes you have made. – T Kambi Jan 23 '17 at 16:54

1 Answers1

0

Dojo must be configured correctly to use both local and CDN sources. Following is the code that correctly loads content from both the CDN and the local server:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://js.arcgis.com/3.19/esri/css/esri.css">
<script>
    var dojoConfig = {
        paths: { js: location.pathname.replace(/\/[^/]+$/, "") + "/js" }
    };
</script>
<script src="https://js.arcgis.com/3.19"></script>
<script>
    require([
        "js/SearchExtent",
        "dojo/domReady!"
    ], function (SearchExtent) {
        console.log("...");
    });
</script>
</head>
<body>
</body>
</html>

The paths property of dojoConfig is used to specify the location of the "js" alias on the local server. Also note that there is not an explicit reference to the custom JavaScript module. The call to require references the custom module using the alias defined in the paths property.

Jonathan Bailey
  • 306
  • 2
  • 13