Window.navigator or just a navigator?

Which object should I use to determine browser information?

alert(window.navigator.userAgent); 

or

 alert(navigator.userAgent); 

Are there any recommendations regarding multi-browser compatibility of the solution?

+9
source share
3 answers

Or it does not really matter. navigator is a property of the window object, but all properties of the window object are available as global variables.

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

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

+21
source

you can use this ....

  alert("You're using " + navigator.appName); 

for pls links follow this navigator link

+1
source

I just ran into a problem in Firefox where a link to navigator.userAgent throws an NS_ERROR_NOT_AVAILABLE exception, while window.navigator.userAgent just works. It seems to be best to use window.navigator.

0
source

Source: https://habr.com/ru/post/901436/


All Articles