devicemotionevent code example

Example: devicemotionevent

window.onload = function () {

  // Check if is IOS 13 when page loads.
  if ( window.DeviceMotionEvent && typeof window.DeviceMotionEvent.requestPermission === 'function' ){

      // Everything here is just a lazy banner. You can do the banner your way.
      const banner = document.createElement('div')
      banner.innerHTML = `

Click here to enable DeviceMotion

` banner.onclick = ClickRequestDeviceMotionEvent // You NEED to bind the function into a onClick event. An artificial 'onClick' will NOT work. document.querySelector('body').appendChild(banner) } } function ClickRequestDeviceMotionEvent () { window.DeviceMotionEvent.requestPermission() .then(response => { if (response === 'granted') { window.addEventListener('devicemotion', () => { console.log('DeviceMotion permissions granted.') }, (e) => { throw e } )} else { console.log('DeviceMotion permissions not granted.') } }) .catch(e => { console.error(e) }) }

Tags:

Misc Example