动态加载JavaScript [英] Load JavaScript dynamically

查看:106
本文介绍了动态加载JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页和一个嵌入了谷歌地图的画布。我在这个网站上使用jQuery。

I have a web page and a canvas with Google Maps embedded in it. I am using jQuery on this site.

我只想在用户点击显示地图时加载Google Maps API。此外,我想从标题中删除整个Google地图的加载,以提高我的网页效果。

I want to load Google Maps API only if the user clicks on "Show me the map". Further, I want to take away the whole loading of the Google Maps from the header in order to improve my page performance.

所以我需要动态加载JavaScript。我可以使用什么JavaScript功能?

So I need to load JavaScript dynamically. What JavaScript function I can use?

推荐答案

您可能想要使用 jQuery.getScript ,可帮助您在需要时加载Google Maps API javascript文件。

You may want to use jQuery.getScript which will help you load the Google Maps API javascript file when needed.

示例:

$.getScript('http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=true', function(data, textStatus){
   console.log(textStatus, data);
   // do whatever you want
});

这篇关于动态加载JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆