未捕获的TypeError:JavaScript中的非法调用(地理位置) [英] Uncaught TypeError: Illegal invocation in Javascript (geolocation)

查看:162
本文介绍了未捕获的TypeError:JavaScript中的非法调用(地理位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行代码时,我收到Javascript错误未捕获的TypeError:非法调用

I am getting the Javascript error "Uncaught TypeError: Illegal invocation" when running the code

var nativeGeoloation = window.navigator.geolocation.getCurrentPosition;
nativeGeoloation(function (){ alert("ok")});

我尝试在窗口的上下文中调用代码,但是得到相同的错误:

I have tried calling the code in the context of the window, but get the same error:

nativeGeoloation.call(window,function (){ alert("ok")})

这个问题的背景是我试图访问由另一个JavaScript库(cordova)覆盖的地理定位功能的本机版本

The background of this question is that I am trying to access the native version of the geolocation function that has been overwritten by another javascript library (cordova)

推荐答案

我在Firefox中遇到的错误是:

The error I'm getting in Firefox is:


TypeError:'getCurrentPosition'在未实现接口地理定位的对象上调用。

TypeError: 'getCurrentPosition' called on an object that does not implement interface Geolocation.

将代码更改为:

var nativeGeoloation = window.navigator.geolocation;
nativeGeoloation.getCurrentPosition(function (){ alert("ok")});

(请注意,您还拼写了 nativeGeoloation 不正确,如果您开始正确拼写,可能会在以后造成问题)。

(Note, you've also spelled nativeGeoloation incorrectly which might cause you problems down the road if you start to spell it correctly).

演示

这篇关于未捕获的TypeError:JavaScript中的非法调用(地理位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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