Thursday, July 24, 2008

Get Version of Internet Explorer using Java Script

This is the code to get the version of Internet Explorer used by the user. If you use navigator.appVersion you will get value 4 for both ie6 & ie7.
<script type="text/javascript" language="javascript">
//Arguments:
//This function is same as explode function in php.
//It takes input string as a string to be exploded.
//separators as a string separator.
//includeEmpties count of array you want as a return (Not required in our case)
//Return:
//It returns the array of strings(Separated by 'separators')

function explode(inputstring, separators, includeEmpties) {
inputstring = new String(inputstring);
separators = new String(separators);
if(separators == "undefined") {
separators = " :;";
}

fixedExplode = new Array(1);
currentElement = "";
count = 0;

for(x=0; x < inputstring.length; x++) {
char = inputstring.charAt(x);
if(separators.indexOf(char) != -1) {
if ( ( (includeEmpties <= 0) || (includeEmpties == false)) && (currentElement == "")) { }
else {
fixedExplode[count] = currentElement;
count++;
currentElement = ""; } }
else { currentElement += char; }
}

if (( ! (includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
fixedExplode[count] = currentElement; }
return fixedExplode;
}
// explode function ends here

//Actual logic starts here ..
full_string = navigator.userAgent;
string_array = explode(full_string,";",10);
version_number = explode(string_array[1]," ",2);
main_version = explode(version_number[2],".",3);
if(main_version[0] == 6){
alert("this is ie6");
}else{
if(main_version[0] == 7){
alert("this is ie7");
}else{
alert("this is other browser");
}
}
</script>

No comments: