javascript - 如何避免Google jsapi加载阻塞网页加载

标签 javascript jquery html gwt google-api

我们有一个 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/

相关文章:

javascript - appendChild 在 FireFox 中似乎表现不佳

javascript - 历史 HTML5 和页面源代码

javascript - 获取 parent 的 sibling 号码去它的 child 被点击

jquery - 更改选项卡式内容部分中选定选项卡的外观

php - 是否可以在不在服务器端使用任何脚本语言的情况下构建 cms?

javascript - 如何使用变量 (B) 中的值选择数组 (A) 中的变量

javascript - 考虑到我们已经有了 `setOrigin` ,那么 "Phaser.Display.Align.In.Center"是用来做什么的呢?

javascript - 使用 jQuery 将字段添加到表中的 HTML 表单

html - React useEffect 保存文本区域文本

html - 在信息框中使用 closeclick 事件