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 rate this snippet

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

Please rate this snippet

How to detect client OS in Javascript

To detect client OS in Javascript you can use the following snippet.

Sample JS

function getOS() {

    if (navigator.appVersion.indexOf("Windows NT 6.3") != -1) {
        return "Windows 8.1";
    }

    if (navigator.appVersion.indexOf("Windows NT 6.2") != -1) {
        return "Windows 8";
    }

    if (navigator.appVersion.indexOf("Windows NT 6.1") != -1) {
        return "Windows 7";
    }

    if (navigator.appVersion.indexOf("Windows NT 6.0") != -1) {
        return "Windows Vista";
    }

    if (navigator.appVersion.indexOf("Windows NT 5.1") != -1) {
        return "Windows XP";
    }

    if (navigator.appVersion.indexOf("Windows NT 5.0") != -1) {
        return "Windows 2000";
    }

    if (navigator.appVersion.indexOf("Windows NT 4.0") != -1) {
        return "Windows NT 4.0";
    }

    if (navigator.appVersion.indexOf("Windows NT 3.51") != -1) {
        return "Windows NT 3.51";
    }

    if (navigator.appVersion.indexOf("Windows NT 3.5") != -1) {
        return "Windows NT 3.5";
    }

    if (navigator.appVersion.indexOf("Windows NT 3.1") != -1) {
        return "Windows NT 3.1";
    }

    if (navigator.appVersion.indexOf("Windows ME") != -1) {
        return "Windows ME";
    }

    if (navigator.appVersion.indexOf("Windows 98") != -1 || navigator.appVersion.indexOf("Win98") != -1) {
        return "Windows 98";
    }
    if (navigator.appVersion.indexOf("Windows 95") != -1 || navigator.appVersion.indexOf("Win95") != -1 || navigator.appVersion.indexOf("Windows_95") != -1) {
        return "Windows 95";
    }

    if (navigator.appVersion.indexOf("Win16") != -1) {
        return "Windows 3.11";
    }

    if (navigator.appVersion.indexOf("Mac_PowerPC") != -1 || navigator.appVersion.indexOf("Mac") != -1 || navigator.appVersion.indexOf("Macintosh") != -1) {
        return "Mac/iOS";
    }
    if (navigator.appVersion.indexOf("OpenBSD") != -1) {
        return "OpenBSD";
    }

    if (navigator.appVersion.indexOf("QNX") != -1) {
        return "QNX";
    }

    if (navigator.appVersion.indexOf("BeOS") != -1) {
        return "BeOS";
    }

    if (navigator.appVersion.indexOf("BeOS") != -1) {
        return "BeOS";
    }

    if (navigator.appVersion.indexOf("SunOS") != -1) {
        return "SunOS";
    }

    if (navigator.appVersion.indexOf("OS/2") != -1) {
        return "OS/2";
    }

    if (navigator.appVersion.indexOf("Linux") != -1) {
        return "Linux";
    }

    if (navigator.appVersion.indexOf("nuhk") != -1 || navigator.appVersion.indexOf("Googlebot") != -1 || navigator.appVersion.indexOf("Yammybot") != -1 || navigator.appVersion.indexOf("Openbot") != -1 || navigator.appVersion.indexOf("Slurp") != -1 || navigator.appVersion.indexOf("MSNBot") != -1 || navigator.appVersion.indexOf("Ask Jeeves/Teoma") != -1 || navigator.appVersion.indexOf("ia_archiver") != -1){
        return "Search Bot";
    }
    return "Unknown OS";
}

Please rate this snippet