【问题标题】:Uncaught ReferenceError: my function is not defined未捕获的 ReferenceError:我的函数未定义
【发布时间】:2012-02-27 17:40:41
【问题描述】:

我正在尝试构建一个 CakePHP 应用程序,它允许用户更新他们的地址信息,然后将新的纬度/经度存储在数据库中。我正在使用 Google 的地理编码器来检索纬度/经度信息,但我的功能不起作用。我几乎没有使用 Javascript 的经验,所以我不确定我做错了什么。我的代码是我在互联网上看到的内容的一种融合。请,如果有人可以建议我,那就太好了。

这是我的地理编码功能:

<script type="text/javascript">

function getLatLong(address){
  var geo = new google.maps.Geocoder;
  var address =<?php echo $this->data['Location']['address'].' '.$this->data['Location']['city'].', '.$this->data['Location']['state'].' '.$this->data['Location']['zip']; ?>;
  geo.geocode({'address':address},function(results, status){
          if (status == google.maps.GeocoderStatus.OK) {
            return results[0].geometry.location;
            alert('The address is' + address);
          } else {
            alert("Geocode was not successful for the following reason: " + status);
          }

   });

  }
</script>

在我的 html 之上,这里是在页面的 onLoad 中调用它的脚本:

<script>
if(window.attachEvent) {
window.attachEvent('onload', getLatLong());
} else {
if(window.onload) {
    var curronload = window.onload;
    var newonload = function() {
        curronload();
        getLatLong();
    };
    window.onload = newonload;
} else {
    window.onload = getLatLong();
}
}
</script>

我的文档开头有:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 

当我加载页面并使用 chrome 检查时,我看到了

Uncaught ReferenceError: getLatLong is not defined   (anonymous function)1:89
1:106      Uncaught SyntaxError: Unexpected identifier

【问题讨论】:

    标签: javascript cakephp-2.0


    【解决方案1】:

    您应该查看生成的代码。我敢打赌是

    var address = address here;
    

    您必须将 PHP 输出放在引号中以使其成为有效的 JavaScript 字符串:

    var address = "<?php echo ... ?>"; 
    

    【讨论】:

    • 听起来不错,我试过了,但没用……意外的标识符错误消失了,但仍然没有定义“getLatLong”。
    • 确保在使用之前定义函数,即将它作为事件处理程序附加。其次,您不必调用函数并将其返回值作为事件处理程序传递,而是必须传递/分配一个引用。 IE。应该是window.attachEvent('onload', getLatLong);window.onload = getLatLong;
    • 我搞定了。请看下面我的回答。感谢您的帮助!
    【解决方案2】:

    对于任何可能遇到此问题的人,我通过更改我的 javascript 函数使其工作,如下所示:

    <script type="text/javascript" >
    var geocoder;
    function getLatLong(address){
    
      geocoder = new google.maps.Geocoder();
      var add = document.getElementById("LocationAddress");
    
      var address =add.value;
      geocoder.geocode({ 'address':address},function(results, status){
              if (status == google.maps.GeocoderStatus.OK) {
                document.getElementById("lat").innerHTML="<strong>"+results[0].geometry.location.lat()+"</strong><br />";
                document.getElementById("lng").innerHTML="<strong>"+results[0].geometry.location.lng()+"</strong>";
    
              } else {
                alert("Geocode was not successful for the following reason: " + status);
              }
    
       });
    

    }

    我之前用过的地方

    return results[0].geometry.location;
            alert('The address is' + address);
    

    当我用 document.write(results[0]geometry.location); 替换它时,它不起作用我的警报开始起作用了。我还确保在我的函数之前定义了一个名为 geocoder 的 var。

    【讨论】:

    • 好吧,如果你从一个函数中return,函数就会终止。不执行以下语句。在其他语言中也是如此。你可以换行,它也可以工作,尽管你不需要 return 语句。
    • 是的,我已经“返回”了,直到我弄清楚我将如何实际使用我得到的数据。我是编程菜鸟,对不起!
    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多