JavaScript String indexOf

var p = navigator.platform;
system.win = p.indexOf(“Win”) == 0;
system.mac = p.indexOf(“Mac”) == 0;
system.x11 = (p.indexOf(“X11”) == 0) || (p.indexOf(“Linux”) == 0);


This code uses the indexOf() method to look at the beginning of the platform string. To detect
Windows, the platform-detection code simply looks for the string “Win” at the beginning of the
platform string (covers both “Win32” and “Win64”). Testing for a Mac platform is done in the same
way to accommodate both “MacPPC” and “MacIntel”. The test for Unix looks for both “X11” and
“Linux” at the beginning of the platform string to future-proof this code against other variants.

<script type="text/javascript">
var mURL = "http://www.google.com/";
var mPosition = mURL.indexOf("www");

document.write("The position of www  =  " + mPosition); 
</script>