<!-- ArcGIS JSAPI --> <script async="false" src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.4compact/"></script> <!-- my application --> <script async="false" src="js/index.js"></script>
Uncaught ReferenceError: require is not defined
var app; require([ "dojo/ready",
function loadScripts(javascripts) { console.debug("Loading JS"); var arcgisJs = [config.arcgisServerUrl + "/jsapi/arcgis/3.4compact/"], allJs = arcgisJs.concat(javascripts), // add other scripts, in this case, index.js head = document.querySelector("head"); allJs.forEach(function (jsUrl) { var tag = document.createElement("script"); tag.setAttribute("async", false); tag.setAttribute("src", jsUrl); head.appendChild(tag); }); console.debug("Finished loading JS"); }Solved! Go to Solution.
function waitForRequireToBeDefinedThenProceed() { if ("undefined" == typeof require) { console.debug("'require' still not defined. Waiting ..."); setTimeout(waitForRequireToBeDefinedThenProceed, 50); } else { console.debug("'require' finally defined. Moving on."); defineAndInitializeApp(); } } function waitForRequireToBeDefinedThenProceed() { if ("undefined" == typeof require) { console.debug("'require' still not defined. Waiting ..."); setTimeout(waitForRequireToBeDefinedThenProceed, 50); } else { console.debug("'require' finally defined. Moving on."); defineAndInitializeApp(); } }I got it working with a bit of a hack suggested by a coworker. Apparently there is some asynchrony going on: require is eventually defined, but it isn't at the time my app initialization code starts running. So I check the "require" variable every 50 ms and move on when it finally is defined:function waitForRequireToBeDefinedThenProceed() { if ("undefined" == typeof require) { console.debug("'require' still not defined. Waiting ..."); setTimeout(waitForRequireToBeDefinedThenProceed, 50); } else { console.debug("'require' finally defined. Moving on."); defineAndInitializeApp(); } }
I should still be glad for a more elegant solution if anyone has a suggestion.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://community.esri.com//js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="https://community.esri.com//js.arcgis.com/3.9/js/esri/css/esri.css">
<script>
function onLoadScript() {
'use strict';
require(
[
'dojo/parser',
'esri/map'
],
function () {
function init() {
var map,
service;
map = new esri.Map('map');
service = new esri.layers.ArcGISTiledMapServiceLayer(window.location.protocol + '//server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer');
map.addLayer(service);
}
dojo.parser.parse();
init();
}
);
}
function loadScript(src, callback) {
'use strict';
var script,
scriptsrc,
scriptTag;
script = document.createElement('script');
script.type = 'text/javascript';
scriptsrc = window.location.protocol;
scriptsrc += '//' + src;
script.src = scriptsrc;
script.onreadystatechange = function () {
if (this.readyState === 'complete' || this.readyState === 'loaded') {
callback();
}
};
script.onload = callback;
scriptTag = document.getElementsByTagName('script')[0];
scriptTag.parentNode.insertBefore(script, scriptTag);
}
loadScript('js.arcgis.com/3.9compact', onLoadScript);
</script>
</head>
<body class="claro">
<div id="map"></div>
</body>
</html>