简体   繁体   English

使用JavaScript禁用地理位置浏览器

[英]Disable geolocation browser using javascript

I'm writing an integration test using QUnit, and in my webapp if geolocation is not available, (I use if (navigator.geolocation) to detect if is present or not, but I want to disable it in order to implement the test when geolocation is not available. 我正在使用QUnit编写集成测试,并且在我的web应用程序中,如果无法使用地理位置,(我使用if (navigator.geolocation)来检测是否存在,但是我想禁用它以便在以下情况下实施测试无法使用地理位置。

I tried navigator.geolocation = undefined but unfortunately it didn't work. 我尝试了navigator.geolocation = undefined但是不幸的是它没有用。

You can't really "disable" navigator.geolocation since it's a read-only property. 您不能真正“禁用” navigator.geolocation因为它是一个只读属性。

Instead, you could create a wrapper that checks for certain functionality and then mock it up for your tests. 相反,您可以创建一个包装器,以检查某些功能,然后对其进行模拟以进行测试。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM