javascript Jquery $(window).load() 不适用于 Chrome、Opera 和 Safari
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8335269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery $(window).load() not working on Chrome, Opera and Safari
提问by william
I would like to explain my situation.
我想说明一下我的情况。
I am writing a website and one of the webpage is using the google map.
我正在编写一个网站,其中一个网页正在使用谷歌地图。
The web site is a cms
website and it is using ektron
该网站是一个cms
网站,它正在使用ektron
So, I have to use ektron google map
coz it provides some features that the customers want.
所以,我必须使用ektron google map
因为它提供了客户想要的一些功能。
Ektron
is simply using google map
and use ajax to combine with its own logic.
Ektron
简单来说就是用google map
ajax结合自己的逻辑。
I dun know the details but whenever I tried to change the properties of that ektron map
, I need to click search
or zoom in
or zoom out
or move the map.
我逼债知道细节,但每当我试图改变的特性ektron map
,我需要点击search
或zoom in
或zoom out
或移动地图。
Only after I do that, the properties changed are effected. If not, they don't.
只有在我这样做之后,更改的属性才会生效。如果没有,他们就没有。
So, I think of the shortcut and programatically cick the search
button of ektron map
.
因此,我想到了快捷方式并以编程方式search
单击ektron map
.
Then again, I am adding the control programatically to a panel
.
然后,我以编程方式将控件添加到panel
.
I can't write document.ready
coz I found out that works before the map is loaded.
我不能写,document.ready
因为我在加载地图之前发现它可以工作。
So, I wrote this code to make the map works.
所以,我写了这段代码来使地图工作。
<script type="text/javascript">
$(window).load(function () {
$("#__GetAddr").click();
});
</script>
It works. __GetAddr
is the search button ID and I click it after everything loads.
有用。__GetAddr
是搜索按钮 ID,我在所有内容加载后单击它。
But the problem started when I use it with chrome.
但是当我将它与 chrome 一起使用时,问题就开始了。
I found out that $(window).load()
is not working in chrome, safari and opera.
我发现这$(window).load()
不适用于 chrome、safari 和 opera。
I googled it for a while but can't find any solid answer.
我用谷歌搜索了一段时间,但找不到任何可靠的答案。
Is there any clear alternative way for that to work in those 3 browsers while it still works for IE7,8,9 and Firefox.
是否有任何明确的替代方法可以在这 3 个浏览器中工作,同时它仍然适用于 IE7、8、9 和 Firefox。
Thanks a lot.
非常感谢。
Hi all, I have a solution.
大家好,我有一个解决方案。
I dun think it's the best solution. but it works.
我不认为这是最好的解决方案。但它有效。
<script type="text/javascript">
if (navigator.appName == "Microsoft Internet Explorer") {
$(window).load(function () { $("#__GetAddr").click(); });
}
else {
window.addEventListener('load', function () { $("#__GetAddr").click(); }, false);
}
</script>
I would appreciate to have a better one. :P
我很感激有一个更好的。:P
回答by william
<script type="text/javascript">
if (navigator.appName == "Microsoft Internet Explorer") {
$(window).load(function () { $("#__GetAddr").click(); });
}
else {
window.addEventListener('load', function () { $("#__GetAddr").click(); }, false);
}
</script>
回答by prashanth padala
You're using jQuery version 3.1.0 and the load event is deprecated for use since jQuery version 1.8. The load event is removed from jQuery 3.0. Instead, you can use on method and bind the JavaScript load event:
您使用的是 jQuery 3.1.0 版,并且自 jQuery 1.8 版起不推荐使用 load 事件。load 事件已从 jQuery 3.0 中删除。相反,您可以使用 on 方法并绑定 JavaScript 加载事件:
$(window).on('load', function () {
$("#__GetAddr").click();
});
回答by Tolga Arican
Did you even try document ready for it?
你甚至尝试过准备好文档吗?
$(document).ready(function() {
$("#__GetAddr").click();
});
回答by lucas clemente
It's not nice, but you could use a timeout if you detect that the browser is not Chrome. However I would really try to get some notification from the map itself, once it is loaded.
这不是很好,但是如果您检测到浏览器不是 Chrome,您可以使用超时。但是,一旦加载,我真的会尝试从地图本身获取一些通知。
回答by Adam Bailin
You could try what Google recommends in their Maps API documentation -- putting an onload event in the body tag. This should work for every browser:
您可以尝试 Google 在他们的 Maps API 文档中推荐的内容——在 body 标签中放置一个 onload 事件。这应该适用于每个浏览器:
<body onload="ready()">
Then, declare the ready() function below in a script tag:
然后,在脚本标签中声明下面的 ready() 函数:
<script type="text/javascript">
function ready() {
$("#__GetAddr").click();
}
</script>