在单独的HTML文件中使用Javascript函数

在单独的HTML文件中使用Javascript函数,javascript,html,parameter-passing,ejs,Javascript,Html,Parameter Passing,Ejs,我知道这似乎是一个愚蠢的问题,但即使有我看过的所有例子,我也不能让它太有效。 我的Javascript文件中有以下代码 server.js // Google Maps Locaton googleMapsClient.geocode({ address: city }, function(err, response) { if (!err) { basicLocation = response.json.results[0].formatted_add

我知道这似乎是一个愚蠢的问题,但即使有我看过的所有例子,我也不能让它太有效。 我的Javascript文件中有以下代码

server.js

  // Google Maps Locaton 
  googleMapsClient.geocode({
    address: city
  }, function(err, response) {
    if (!err) {
      basicLocation = response.json.results[0].formatted_address;
      console.log(basicLocation);
    }
  });

});

function getBasicLocation() {   // Able to grab location from index.ejs file 
  return basicLocation;
}
在我的HTML中,我试图随着位置的变化更新文本。我的html代码如下所示

index.ejs

<script src="../server.js">
    </script>
      <h3>
        <script> getBasicLocation() </script> Location
      </h3>

如何让getBasicLocation()与index.ejs文件通信,以便它更新文本

以下步骤应该有效-

  • 导入server.js-您正在这样做
  • 在需要的位置创建HTML节点 e、 g div id=“位置”
  • 创建包含以下内容的脚本标记

            let lc = getBasicLocation();
    document.getElementById("location").innerHTML = lc;
    

  • JQuery可以进一步简化您的节点选择代码

    看,它可能有助于您尝试将basicLocation作为全局变量
            let lc = getBasicLocation();
    document.getElementById("location").innerHTML = lc;