如何在未加载库时调试引用错误?

时间:2014-10-25 14:39:07

标签: javascript html leaflet

例如我得到一个reference error L is not defined,其中L是传单。我跟着their tutorial,这就是我身体结束前的情况:

<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script>
    var map = L.map('map').setView([51.505, -0.09], 13);
    L.tileLayer('http://{s}.tiles.mapbox.com/v4/secretid/{z}/{x}/{y}.png', {
      attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
      maxZoom: 18
    }).addTo(map);
</script>

我已经检查过了js文件的超链接。

1 个答案:

答案 0 :(得分:1)

为确保所有脚本都已加载,请使用onload回调,如下所示:

<script>
    window.onload = function() {
     var map = L.map('map').setView([51.505, -0.09], 13);
     L.tileLayer('http://{s}.tiles.mapbox.com/v4/secretid/{z}/{x}/{y}.png', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a     href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a     href="http://mapbox.com">Mapbox</a>',
 maxZoom: 18
    }).addTo(map);
}
</script>