org.mozilla.javascript.EcmaError:ReferenceError:\“document \”未定义

时间:2014-11-18 16:03:22

标签: javascript google-maps

我正在使用谷歌地图获取位置的坐标。我的代码运行正是我想要的,但是当我在表单上单击“保存”时,我会在上面的标题中看到错误。

谁能告诉我哪里出错了?

function init(){
    //Calls the loadScript and initialize function and loads the Google Maps API 
    loadScript('//maps.googleapis.com/maps/api/js?APIKEY&callback=initialize');
}

function loadScript(src,callback){
    //Adds the google maps script to the head of the HTML
    alert('loading');
    var script = document.createElement("script");
    script.type = "text/javascript";
    if(callback)script.onload=callback;
    document.getElementsByTagName("head")[0].appendChild(script);
    script.src = src;
  }

1 个答案:

答案 0 :(得分:1)

显然,我所需要的只是尝试捕捉代码:

function loadScript(src,callback){
    //Adds the google maps script to the head of the HTML
    try{
    var script = document.createElement("script");
    script.type = "text/javascript";
    if(callback)script.onload=callback;
    document.getElementsByTagName("head")[0].appendChild(script);
    script.src = src;
    }catch(e){
        alert(e);
    }
  }