javascriptcross-browser

window.navigator or just navigator?


What object I should use to determine browser's info?

alert(window.navigator.userAgent);

or

alert(navigator.userAgent);

Is there any recomendations about cross-browser compatibility of a decision?


Solution

  • Either, it doesn't really matter. navigator is a property of the window object, but all properties of the window object are accessible as global variables.

    navigator === window.navigator;
    //-> true
    

    As a personal preference, I always write window.propertyName for explicit properties of the window object.