Android 浏览器在页面重新加载时不会加载 Google Maps API
我正在使用此函数动态加载 Google Maps API:
App.prototype.loadScript = function(src) {
$(document.createElement("script")).attr({
type: "text/javascript",
src: src
}).appendTo("body");
};
我将此 URL 作为参数传递:
"//maps.googleapis.com/maps/api/js?v=3.8&sensor=false&language=" + locale + "&callback=window.app.googleMapsCallback"
它在我第一次加载页面时工作正常,并且会触发回调。但是,当我在 Android 上重新加载页面时,window.app.googleMapsCallback 不会触发。它在 Chrome 和 iOS 浏览器中运行良好。
就像脚本被缓存一样,只是不会再次重新加载。我尝试在 URL 中添加时间戳参数,但这没有帮助。
如何确保回调始终触发?
更新:
经过进一步测试,清除缓存和重新加载也不起作用。我必须清除所有浏览器数据(通过“设置”->“应用程序”->“管理应用程序”)才能成功重新加载页面。
使用顶级函数作为回调没有什么区别。
重新加载时没有 JavaScript 错误。
我应该提到的是,我使用的是 Android 2.3.4 的 HTC Evo 4G,并且我的应用程序是使用 jQuery Mobile 构建的。
I'm loading Google Maps API dynamically using this function:
App.prototype.loadScript = function(src) {
$(document.createElement("script")).attr({
type: "text/javascript",
src: src
}).appendTo("body");
};
I pass this URL as the argument:
"//maps.googleapis.com/maps/api/js?v=3.8&sensor=false&language=" + locale + "&callback=window.app.googleMapsCallback"
It works fine the first time I load the page, and the callback fires. However, when I reload the page on Android, window.app.googleMapsCallback does not fire. It works fine in Chrome and in the iOS browser.
It's like the script is cached, and is just not reloaded again. I've tried adding a timestamp parameter to the URL but that didn't help.
How can I make sure the callback always fires?
UPDATE:
Upon further testing, clearing the cache and reloading doesn't work either. I have to clear out all browser data (via Settings -> Applications -> Manage applications) to reload the page successfully.
Using a top level function as a callback makes no difference.
There are no JavaScript errors on reload.
I should mention that I'm using an HTC Evo 4G with Android 2.3.4, and my application is built with jQuery Mobile.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我一直在尝试使用 Android 模拟器(版本 2.3.3 和 2.3.5。没有 2.3.4 版本,抱歉)重现您的问题,但我无法做到。回调在每次页面重新加载时运行良好。
这是测试jsfiddle:http://jsfiddle.net/MartinodF/gbAuL/。它仅包含用于测试您的问题的最少代码,并排除它取决于代码库中的其他内容。
你能在你的手机上试试吗?单击 jsfiddle 中的“在移动设备上调试”(顶部栏中“运行”右侧的按钮),然后打开它为您提供的 url(类似于 http://jsfiddle.net/m/ABC)。您应该会看到一个弹出窗口,显示“回调已调用”。尝试重新加载页面,看看是否有效。
如果没有,那么我可以尝试使用 2.3.4、HTC Sense 皮肤设备进行进一步测试。
I've been trying to reproduce your problem using the Android emulator (both versions 2.3.3 and 2.3.5. there is no 2.3.4 version, sorry), but I haven't been able to. The callback runs fine on each page reload.
This is the test jsfiddle: http://jsfiddle.net/MartinodF/gbAuL/. It only includes the minimal code to test your problem and to exclude it depends on something else in your code base.
Could you please try it on your phone? Click "Debug on mobile" (the button on the right of "Run" in the top bar) in jsfiddle, and open up the url it gives you (something like http://jsfiddle.net/m/ABC) in the Android browser. You should get a popup saying "callback called". Try to reload the page, and see if it works.
If it doesn't, then I can try and get my hands on a 2.3.4, HTC Sense skinned device for further testing.