javascript window.onload 工作但 Chrome 控制台说:未捕获的类型错误:window.onload 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29927638/
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
window.onload work but Chrome console says: Uncaught TypeError: window.onload is not a function
提问by Yoda
I want to run getLocation()
method at the page load. I added: window.onload(getLocation());
and the function is invoked as I desire but Chrome console says:
我想getLocation()
在页面加载时运行方法。我补充说:window.onload(getLocation());
该函数是按我的意愿调用的,但 Chrome 控制台说:
Uncaught TypeError: window.onload is not a function(anonymous function) @ (index):116
The view, the window.onload(getLocation());
is at the bottom:
视图,window.onload(getLocation());
位于底部:
@{
ViewBag.Title = "Home Page";
}
<div id="demo"></div>
<h2>Gecoding Demo JavaScript: </h2>
<div id="map" style="height: 253px ; width: 253px" />
@section Scripts {
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
var position = navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
InitializeMap(position)
}
var map;
var geocoder;
function InitializeMap(position) {
alert(position.coords.latitude+"");
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var myOptions =
{
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
}
window.onload(getLocation());
</script>
}
回答by Trott
The way you've written your code, it's not running onload, it's just running when the parser hits it. Because you wrote getLocation()
rather than just getLocation
, it executes the function.
按照您编写代码的方式,它不是在加载时运行,而是在解析器命中它时运行。因为你写的getLocation()
不仅仅是getLocation
,它会执行函数。
If you are certain there will be nothing else to be fired on load, you can do window.onload=getLocation;
. If you want to make sure you play nicely with other code (including third-party frameworks/libraries) that might use the load event, you can do something like this:
如果您确定在加载时不会有任何其他事情可触发,则可以执行window.onload=getLocation;
. 如果您想确保与可能使用 load 事件的其他代码(包括第三方框架/库)配合得很好,您可以执行以下操作:
window.addEventListener('load', getLocation);
Note that that code won't work in IE8. If you need to support IE8, check for addEventListener()
and if it is not found, check for and use attachEvent()
instead:
请注意,该代码在 IE8 中不起作用。如果您需要支持 IE8,请检查addEventListener()
,如果没有找到,请检查并使用attachEvent()
:
if (window.addEventListener) {
window.addEventListener('load', getLocation);
} else if (window.attachEvent) {
window.attachEvent('onload', getLocation);
} else {
window.onload = getLocation;
}