org.mozilla.javascript.EcmaError: ReferenceError: "doc

org.mozilla.javascript.EcmaError: ReferenceError: "document" is not defined

本文关键字:quot doc ReferenceError javascript EcmaError org mozilla      更新时间:2023-09-26

我正在使用谷歌地图来获取位置的坐标。我的代码完全按照我想要的方式运行,但是当我单击表单上的保存时,我在上面的标题中出现错误。

谁能告诉我哪里出错了?

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;
  }
显然,

我所需要的只是尝试捕获代码:

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);
    }
  }