Search code examples
javascriptairconsole

Airconsole Code: navigator.vibrate


I was looking at the airconsole javascript file and found a piece of code and I don't know what it describe. Can anyone please let me know.

navigator.vibrate = (navigator.vibrate ||
                     navigator.webkitVibrate ||
                     navigator.mozVibrate ||
                     navigator.msVibrate);

Solution

  • This line makes the function call to vibrate the device browser-independent.

    The || means or in JavaScript. The code after || will only be executed if the code before the || returns false (This is called short-circuit evaluation). So if the general vibrate function does not exist, it will try the WebKit-specific vibration function (for Safari and other WebKit-based browsers). If that does not exist either, the Firefox specific function, and if that won't work, the Microsoft (Internet Explorer) specific function.

    See also the headline "Browser Support and Detection" in this tutorial on how to use the HTML5 vibration API.