简体   繁体   English

附加<Script> Tag to Body?

[英]Append <Script> Tag to Body?

I want to append a script tag to the body of my HTML page. 我想将脚本标签附加到HTML页面的正文中。 I added the following in my page: 我在页面中添加了以下内容:

<script type="text/javascript">  

    function loadScript() {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'https://maps.googleapis.com/maps/api/js?sensor=false&' +
                'callback=initialize';
        document.body.appendChild(script);
    }

    window.onload = loadScript;

</script>

I tried to run it in JSFidle and there where no problems. 我试图在JSFidle中运行它,那里没有问题。

When I run it on my html page which runs on a demo server I get the following error in the console: 当我在演示服务器上运行的html页面上运行它时,在控制台中出现以下错误:

Uncaught TypeError: undefined is not a function

在此处输入图片说明

What causes this error and how can I prevent it? 是什么导致此错误,我该如何预防?

Edit: Removing the type text/javascript does not change anything. 编辑:删除文本text / javascript不会更改任何内容。

The error you are getting is because there is no initialize() function. 您收到的错误是因为没有initialize()函数。 If you declare one there will be no error. 如果声明一个,就不会有错误。

function loadScript() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize';
    document.body.appendChild(script);
    console.log('loadScript');
}

function initialize() {
    console.log('initialize');
}

window.onload = loadScript;

I tried to run it in JSFidle and there where no problems. 我试图在JSFidle中运行它,那里没有问题。

Incorrect. 不正确 On JSFiddle you have a different problem. 在JSFiddle上,您有另一个问题。

There you run your code when the document load event fires. 当文档加载事件触发时,您在此处运行代码。 That code defines a function and assigns an event handler for the document load event. 该代码定义了一个函数,并为文档加载事件分配了一个事件处理程序。 Since that event has already fired, the function never runs. 由于该事件已经触发,因此该函数永远不会运行。 You don't get an error because the code doesn't get as far as it does when you test in your standalone document. 您不会得到任何错误,因为在独立文档中进行测试时,代码的作用远不如它。

When I run it on my html page which runs on a demo server I get the following error in the console: 当我在演示服务器上运行的html页面上运行它时,在控制台中出现以下错误:

In this case, the code is running successfully. 在这种情况下,代码将成功运行。

The load event fires. 加载事件触发。 The loadScript function runs. loadScript函数运行。 The script element is appended to the page. 脚本元素将附加到页面。 Google's script runs. Google的脚本运行。

You get an error because you said: 您收到错误消息是因为您说:

  'callback=initialize' 

So the script you are requesting from Google tries to call initialize() , but you haven't defined that function so you get an error instead. 因此,您从Google请求的脚本尝试调用initialize() ,但是您尚未定义该函数,因此会出现错误。

See also the documentation which includes this example: 另请参阅包含此示例的文档

function initialize() {
  var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644)
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
}

function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp' +
      '&signed_in=true&callback=initialize';
  document.body.appendChild(script);
}

window.onload = loadScript;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM