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);

How to detect orientation and change the css file in Javascript

To detect orientation and change the css file in Javascript you can use the following snippet.

Sample Javascript

function orient()  
{  
 switch(window.orientation){    
	case 0: document.getElementById("orient_css").href = "css/iphone_portrait.css";  
	break;  
	case 90: document.getElementById("orient_css").href = "css/iphone_landscape.css";  
	break;  
	case -90: document.getElementById("orient_css").href = "css/iphone_landscape.css";  
	break;  
}
window.onload = orient();