Problem definition
I need to put text similar to the following on a web page:
If the user is operating on a Windows based system, I want them to see:
Look for the file C:\Users\yourname\dir1\dir2\filename on your Windows PC
If the user is operating on an OS X based system, I want them to see:
Look for the file /Users/yourname/dir1/dir2/filename on your Macintosh PC
If the user is operating on[any other|Linux] based system, I want them to see:
Look for the file /home/yourname/dir1/dir2/filename on your Linux PC
Research
I have managed to create three almost identical javascripts that partially work. One script gives me the value of the osHomeDir
, another osType
, and the other gives me the value of the osSeparator
. My problem is that I don't know how to get these values out of the java script and into my webpage consistently. What I get is that the first substitution works, but I can't repeat it - for instance, on my Mac I see:
Look for the file /Users/yournamedir1dir2filename on your Macintosh PC rather than the desired: Look for the file /Users/yourname/dir1/dir2/filename on your Macintosh PC
Note how only the FIRST substitution of osSeparator
worked.
Desired outcome
I'm hoping someone can show me two things:
- How to make the scripts more efficient - three almost identical scripts could surely be changed into one that returned three values
- The ability to be able to use the result more than once (as happened
in my example)
Notes
Don't get too concerned about THIS example - I have simplified it to make it readable. I have the same problem in many places. I know I COULD solve this one my returning the whole sentence in the one script, but that would miss the point entirely.
My code
<!DOCTYPE html>
<html>
<body>
<p> Look for the file <a id="osHomeDirId"></a><a id="osSeparatorId"></a>yourname<a id="osSeparatorId"></a>dir1<a id="osSeparatorId"></a>dir2<a id="osSeparatorId"></a>filename on your <a id="osTypeId"></a> PC</p>
<script>
var osHomeDir = "/home";
if (window.navigator.platform.indexOf("Win") != -1) osHomeDir="C:\Users";
if (window.navigator.platform.indexOf("Mac")!=-1) osHomeDir="/Users";
document.getElementById("osHomeDirId").innerHTML=osHomeDir;
</script>
<script>
var osType = "Linux";
if (window.navigator.platform.indexOf("Win") != -1) osType="Windows";
if (window.navigator.platform.indexOf("Mac")!=-1) osType="Macintosh";
document.getElementById("osTypeId").innerHTML=osType;
</script>
<script>
var osSeparator = "/";
if (window.navigator.platform.indexOf("Win") != -1) osSeparator="\";
document.getElementById("osSeparatorId").innerHTML=osSeparator;
</script>
</body>
</html>