How to detect a screen orientation change in Javascript

To detect a screen orientation change in Javascript you can use the following snippet.

Sample Javascript

window.addEventListener("orientationchange", function() {
	//0 = potrait
	//90 or -90 = landscape left or landscape right
}, false);

some devices do not fire the orientationchange event, try the resize event instead.

Sample Javascript

window.addEventListener("resize", function() {
	// check screen size (inner and outer!)	
}, false);

Please Share us:
Please rate this snippet

3 thoughts on “How to detect a screen orientation change in Javascript

Leave a Reply