我们有一个 GWT Web 应用程序,它也使用 Google jsapi 和 jquery。
在index.html中,我们加载了Google jsapi,如下所示:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
但是,当客户端无法访问 Google 时,这似乎会挂起网页的加载,直到超时。在某些国家,例如中国。
我已将上述脚本元素更改如下,不太确定这是否正确:
<script type="text/javascript">
$.ajax({
url: 'https://www.google.com/jsapi',
dataType: 'script',
cache: true
});
</script>
并尝试阻止自己的客户端,使其无法访问 www.google.com,但是,在加载页面时,它仍然等待加载 jsapi。我检查了浏览器控制台的网络日志,有两个Initiators,如下
ajax @ jquery.min.js:127
(anonymous function) @ (index):18
当然,index:18 位于脚本元素之上。但我不明白关于 jquery.min.js:127 的情况,我将其从本地包含在 index.html 中,如下所示
<script type="text/javascript" src="/js/jquery.min.js"></script>
这个 jquery.min.js 是 jQuery v1.4.2,如下 link ,但看起来第 127 行没有加载 google jsapi?
最佳答案
看看 GWT AjaxLoader 。有了这个你就不必添加
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
到您的 HTML。您可以在GWT Java代码中执行jsapi加载。例如,这将加载搜索模块:
AjaxLoaderOptions options = AjaxLoaderOptions.newInstance();
//add options if you want to
AjaxLoader.loadApi("search", "1", new Runnable() {
@Override
public void run() {
//do what you like
}
}, options);
使用这种方法我没有遇到任何加载延迟。
关于javascript - 如何避免Google jsapi加载阻塞网页加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192907/