如何按需加载Google API(UDS.JS)(使用jQuery)?

时间:2022-10-12 21:59:15

Tried this:

$('.link').click(function(e) {
 $.getScript('http://www.google.com/uds/api?file=uds.js&v=1.0', function() {        
  $('body').append('<p>GOOGLE API (UDS) is loaded</p>');
 });
 return false;
});

Yes, it loads a primary "uds.js" file and then locks page by loading a locale JS file ("default+en.I.js", see line #48 in "uds.js").

是的,它加载主“uds.js”文件然后通过加载语言环境JS文件来锁定页面(“default + en.I.js”,参见“uds.js”中的第48行)。

workaround (@jsbin)

1 个解决方案

#1


If you want to dynamically load google's libraries, you should check out google's autoloader: http://code.google.com/apis/ajax/documentation/#AutoLoading

如果您想动态加载谷歌的图书馆,您应该查看谷歌的自动加载器:http://code.google.com/apis/ajax/documentation/#AutoLoading

It works quite nicely, but be careful if you use the autoloader wizard.

它工作得很好,但是如果你使用自动加载器向导要小心。

http://code.google.com/apis/ajax/documentation/autoloader-wizard.html

there's a bug for the c&p code that tripped me up: http://code.google.com/p/google-ajax-apis/issues/detail?id=244

c&p代码有一个错误让我感到困惑:http://code.google.com/p/google-ajax-apis/issues/detail?id = 244

Also I found that for some of google's libraries, if I try to asynchronously load scripts (like yours) that if I don't specify some of the optional parameters (language, callback, etc. -- even with an empty string), I'll see the behavior that you're seeing.

另外我发现对于一些谷歌的库,如果我尝试异步加载脚本(如你的),如果我没有指定一些可选参数(语言,回调等 - 即使是空字符串),我会看到你所看到的行为。

Edit: went ahead and tested it. Your solution here: http://pastie.org/486925

编辑:继续进行测试。您的解决方案:http://pastie.org/486925

#1


If you want to dynamically load google's libraries, you should check out google's autoloader: http://code.google.com/apis/ajax/documentation/#AutoLoading

如果您想动态加载谷歌的图书馆,您应该查看谷歌的自动加载器:http://code.google.com/apis/ajax/documentation/#AutoLoading

It works quite nicely, but be careful if you use the autoloader wizard.

它工作得很好,但是如果你使用自动加载器向导要小心。

http://code.google.com/apis/ajax/documentation/autoloader-wizard.html

there's a bug for the c&p code that tripped me up: http://code.google.com/p/google-ajax-apis/issues/detail?id=244

c&p代码有一个错误让我感到困惑:http://code.google.com/p/google-ajax-apis/issues/detail?id = 244

Also I found that for some of google's libraries, if I try to asynchronously load scripts (like yours) that if I don't specify some of the optional parameters (language, callback, etc. -- even with an empty string), I'll see the behavior that you're seeing.

另外我发现对于一些谷歌的库,如果我尝试异步加载脚本(如你的),如果我没有指定一些可选参数(语言,回调等 - 即使是空字符串),我会看到你所看到的行为。

Edit: went ahead and tested it. Your solution here: http://pastie.org/486925

编辑:继续进行测试。您的解决方案:http://pastie.org/486925