This seems to work rather well, although it can be a little slow to return the value on O365. I get the right versions on both 2010 and O365. But the credit goes here, and they have several other options that also work.
var xmlHttp = null;
if (window.XMLHttpRequest) { // If IE7, Mozilla, Safari, and so on: Use native object.
xmlHttp = new XMLHttpRequest();
}else{
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0'); // ...otherwise, use the ActiveX control for IE5.x and IE6.
}
}
xmlHttp.open('HEAD', location.href, false);
xmlHttp.send();
var headers = xmlHttp.getAllResponseHeaders();
var SPVersion = xmlHttp.getResponseHeader("MicrosoftSharePointTeamServices");
console.log("Full Sharepoint Version ="+SPVersion);
switch(SPVersion.substring(0,2) ){
case "12": console.log("2007"); break;
case "14": console.log("2010"); break;
case "16": console.log("2013"); break;
default: console.log(SPVersion); break;
}
I like this way Again, the credit goes here:
//return 0 if not Sharepoint, otherwise, return major version
function getSharePointMajorVersion(){
// The following copied from: http://msdn.microsoft.com/en-us/library/ms537505(v=vs.85).aspx
var xmlHttp = null;
if (window.XMLHttpRequest) {
// If IE7, Mozilla, Safari, and so on: Use native object.
xmlHttp = new XMLHttpRequest();
}else{
if (window.ActiveXObject) {
// ...otherwise, use the ActiveX control for IE5.x and IE6.
xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');
}
}
xmlHttp.open('HEAD', location.href, false);
xmlHttp.send();
var SPVersion = xmlHttp.getResponseHeader("MicrosoftSharePointTeamServices");
if(SPVersion == null){
return 0;
}else{
return SPVersion.substring(0,2);
}
}
alert( getSharePointMajorVersion() );