window.onload工作,但Chrome控制台说:未捕获的TypeError:window.onload不是一个函数 [英] window.onload work but Chrome console says: Uncaught TypeError: window.onload is not a function

查看:177
本文介绍了window.onload工作,但Chrome控制台说:未捕获的TypeError:window.onload不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面加载时运行 getLocation()方法。我添加了: window.onload(getLocation()); 并根据需要调用该函数,但Chrome控制台说:

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:

 Uncaught TypeError: window.onload is not a function(anonymous function) @ (index):116






视图, 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>
}


推荐答案

你写的方式你的代码,它没有运行onload,它只是在解析器命中它时运行。因为您编写了 getLocation()而不是 getLocation ,所以它会执行该函数。

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.

如果您确定在加载时没有其他任何内容可以解雇,您可以执行 window.onload = getLocation; 。如果你想确保你可以使用可能使用load事件的其他代码(包括第三方框架/库),你可以这样做:

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.addEventListener('load', getLocation);

请注意,该代码在IE8中不起作用。如果您需要支持IE8,请检查 addEventListener(),如果找不到,请检查并使用 attachEvent()而不是:

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:

   if (window.addEventListener) {
      window.addEventListener('load', getLocation);
   } else if (window.attachEvent) {
      window.attachEvent('onload', getLocation);
   } else { 
      window.onload = getLocation;
   }

这篇关于window.onload工作,但Chrome控制台说:未捕获的TypeError:window.onload不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆