javascript - 未捕获的类型错误 : Illegal invocation in Javascript (geolocation)

标签 javascript html cordova geolocation

运行代码时出现 Javascript 错误“Uncaught TypeError:非法调用”

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

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

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

这个问题的背景是我正在尝试访问已被另一个javascript库(cordova)覆盖的 native 版本的地理定位函数

最佳答案

我在 Firefox 中遇到的错误是:

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

将代码更改为:

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

(请注意,您还错误地拼写了 nativeGeoloation,如果您开始拼写正确,可能会在以后出现问题)。

DEMO

关于javascript - 未捕获的类型错误 : Illegal invocation in Javascript (geolocation),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26254123/

相关文章:

javascript - 找不到模块 : Error: Can't resolve 'react-dom/client'

javascript - 如何使用 JQuery 从列值数组创建 HTML 表

javascript - 将 Canvas 数据导出到 json

html - 带有百分比的 chrome 中的 css 边框半径错误

java - OnBootReceiver 可以工作,但找不到 AlarmReceiver

javascript - 使用 textcolor 时如何配置 MathJax 以识别等式中的 textrm{}?

javascript - 将 keyDown 事件添加到 iframe 中的 Canvas

php - 我可以将 php 与 phonegap 一起使用吗?

android - 在 Phonegap 应用程序上使用相机,只有 HTML 输入,没有相机 API

php - 在 jQuery 自动刷新 div 中使用 jQuery 工具提示