gpt4 book ai didi

javascript - 使用 JavaScript 禁用地理定位浏览器

转载 作者:行者123 更新时间:2023-12-03 06:32:32 25 4
gpt4 key购买 nike

我正在使用 QUnit 编写集成测试,并且在我的 web 应用程序中如果地理位置不可用(我使用 if (navigator.geolocation) 来检测是否存在,但我想要禁用它以便在地理定位不可用时实现测试。

我尝试了navigator.geolocation = undefined,但不幸的是它不起作用。

最佳答案

你不能真正“禁用”navigator.geolocation因为它是只读属性。

相反,您可以创建一个包装器来检查某些功能,然后为您的测试模拟它。

var supports = {
geolocation: !!navigator.geolocation // will be `true` if geolocation is defined
};

// In your tests...
supports.geolocation = false;
executeTest();

关于javascript - 使用 JavaScript 禁用地理定位浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38384362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com