Harsaroop Dhillon Harsaroop Dhillon - 15 days ago 5
Java Question

Uncaught ReferenceError: platform is not defined

Yes so i recently picked up javascript as another language to learn. I was working on a friend's website and we need to keep logs of the platform OS so that we can rate our stats.

I get the error Uncaught ReferenceError: platform is not defined at get_platform (eval at (file:///C:/Users/*****/Desktop/Project/index.html:2:9), :1:191)

Does anyone have any idea on how i could go about fixing this? Any help is greatly appreciated!

This is my code.

function get_platform(os_name) {
var os_str = os_name;
var chk_win = os_str.substr(0, 3);
if (chk_win == "Win")
platform = "win";
if (chk_win == "Lin")
platform = "linux";
if (chk_win == "X11")
platform = "unix";
return platform};
function get_os() {
var OSName = "Unknown";
if (window.navigator.userAgent.indexOf("Windows NT 6.3") != -1)
OSName = "Windows 8.1";
if (window.navigator.userAgent.indexOf("Windows NT 6.2") != -1)
OSName = "Windows 8";
if (window.navigator.userAgent.indexOf("Windows NT 6.1") != -1)
OSName = "Windows 7";
if (window.navigator.userAgent.indexOf("Windows NT 6.0") != -1)
OSName = "Windows Vista";
if (window.navigator.userAgent.indexOf("Windows NT 5.1") != -1)
OSName = "Windows XP";
if (window.navigator.userAgent.indexOf("Windows NT 5.0") != -1)
OSName = "Windows 2000";
if (window.navigator.userAgent.indexOf("Mac") != -1)
OSName = "Mac/iOS";
if (window.navigator.userAgent.indexOf("X11") != -1)
OSName = "UNIX";
if (window.navigator.userAgent.indexOf("Linux") != -1)
OSName = "Linux";
return OSName};

Answer

Try this ... it looks like the variable platform hasn't been defined properly.

function get_platform(os_name) {
  var os_str = os_name;
  var chk_win = os_str.substr(0, 3);
  var platform = "";
  if (chk_win == "Win")
    platform = "win";
  if (chk_win == "Lin")
    platform = "linux";
  if (chk_win == "X11")
    platform = "unix";
  return platform
};

Note this line added above; you can define it this way or add a 'default' option:

var platform = "";