最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I call a function that is defined inside of require() outside of the require()? - Stack Overflow

programmeradmin0浏览0评论

I am using ArcGIS API for Javascript 3.21. I have a function inside of the require(). I want the function to be called when a button is clicked, but the button is outside the require().

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//js.arcgis/3.7/js/esri/css/esri.css">
<style>
    html, body, #map {
    height: 100%;
    width: 100%;
    margin: 1;
    padding: 1; 
    }
</style>

<script src="//js.arcgis/3.7/"></script>
<script>

    var map;

    require([
      "esri/map", 
      "esri/geometry/Point",
      "esri/symbols/SimpleMarkerSymbol",
      "esri/graphic", 
      "esri/layers/GraphicsLayer",
      "dojo/domReady!"
    ], function(
      Map, Point, SimpleMarkerSymbol, Graphic, GraphicsLayer
    ) {
      map = new Map("map", {
      basemap: "gray",
      center: [10,10],
      zoom: 3
    });

    map.on("load", function() {
      var graphicslayer = new GraphicsLayer();
      map.addLayer(graphicslayer);
    });

   function hello(){
      alert("hello,world!");
   }   

});



</script>
</head>
<body>

    <button type="submit"class="searchButton"onclick="hello()">Search</button>
    <div id="map"></div>

</body>
</html>

I can't call hello() in onclick="hello()" because hello() is inside the require().

I am using ArcGIS API for Javascript 3.21. I have a function inside of the require(). I want the function to be called when a button is clicked, but the button is outside the require().

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//js.arcgis./3.7/js/esri/css/esri.css">
<style>
    html, body, #map {
    height: 100%;
    width: 100%;
    margin: 1;
    padding: 1; 
    }
</style>

<script src="//js.arcgis./3.7/"></script>
<script>

    var map;

    require([
      "esri/map", 
      "esri/geometry/Point",
      "esri/symbols/SimpleMarkerSymbol",
      "esri/graphic", 
      "esri/layers/GraphicsLayer",
      "dojo/domReady!"
    ], function(
      Map, Point, SimpleMarkerSymbol, Graphic, GraphicsLayer
    ) {
      map = new Map("map", {
      basemap: "gray",
      center: [10,10],
      zoom: 3
    });

    map.on("load", function() {
      var graphicslayer = new GraphicsLayer();
      map.addLayer(graphicslayer);
    });

   function hello(){
      alert("hello,world!");
   }   

});



</script>
</head>
<body>

    <button type="submit"class="searchButton"onclick="hello()">Search</button>
    <div id="map"></div>

</body>
</html>

I can't call hello() in onclick="hello()" because hello() is inside the require().

Share Improve this question asked Aug 14, 2017 at 3:02 ccproccpro 557 bronze badges 1
  • en.wikipedia/wiki/Unobtrusive_JavaScript – zzzzBov Commented Aug 14, 2017 at 3:06
Add a ment  | 

4 Answers 4

Reset to default 4

Your hello function is scoped to the require function. You want to scope it to the global object, which is the window object in your case. So either:

function hello(){
    alert("hello,world!");
}   
window.hello = hello;

or directly

window.hello = function(){
    alert("hello,world!");
}

But you could also bind your hello function to the click event of your object directly in javascript; you don't have to widen the scope of your function. There is likely methods to do so in the dojo library. A direct javascript way could be something like

var myButton = document.querySelectorAll("button.searchButton")[0];
if (myButton) {
    myButton.addEventListener("click", hello);
}

any function inside require({....}), will NOT be visible to onclick='xxx('bbb')'

You have 2 choice:

1) move this function outside require({....})

2) if the function has to be inside require({....}), then you have to make it as global function by ( as @Nicolas said)

window.xxx = function(bbb){
        alert(bbb);
}

You could also use the dojo module "On" :

Add this to your require init :

require([...,"dojo/on",...], function(...,on,...) {

Now code your event handler (assuming you add an id to your button) :

<button type="submit" class="searchButton" id="searchButton">Search</button>
on(dojo.byId('searchButton'), 'click', function(evt) {
  alert('hello');
});

you can't call a function inside a require to use later, because hello function is inside an anonymous function, which require function runs once, and it's not possible to recover what is inside of require function.

so, hello must be outside from require stament if u want call hello whenever you need it.

发布评论

评论列表(0)

  1. 暂无评论