Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

javascript - Device Orientation API returns different values for desktop browsers

Using the Device Orientation API demo on desktop Firefox 44 and Chrome Version 47.0.2526.80 m return different results for the coordinates.

Firefox returns null and Chrome 0. So far so good.

With this piece of code I am trying to check deviceorientation data.

So first I check if the Device Orientation event is supported and both Firefox and Chrome say it is with the first alert firing.

if(window.DeviceOrientationEvent) {
  alert("Device Orientation Event is supported.");

  window.addEventListener('deviceorientation', function(event) {
    var alpha = event.alpha;
    var beta = event.beta;
    var gamma = event.gamma;

    // https://stackoverflow.com/a/27550756
    if(alpha === null && typeof alpha === "object")
      alert(alpha);
  }, false);
}

However only Chrome correctly returns the second alert as null whereas Firefox does not trigger the alert at all. But it should do so looking at the data results from the Device Orientation API demo.

I have also made sure to specifically check for null and not "", undefined, false, 0, or NaN as per How do I check for null values in JavaScript?

So how do I get Firefox to actually fire the second alert with the data of the deviceorientation ?

Related questions:

Device Orientation not working firefox

'deviceorientation' in Firefox?

Different gyroscope values in chrome mobile and safari mobile

Testing hardware support in Javascript for device orientation events of the iPhone 3GS

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...