问题
Is it possible to change the destination of a link based on whether the user has a mac or a PC using javascript?
To give an example: The Apple website allows download of Quicktime but it "knows" whether you are using a mac or a pc and directs you to the relevant page.
Background/Reason for doing this: I have built a website for someone and they have a number of audio and video files on there. Ideally they want it so that if the user is on a mac it will download a quicktime version of the file but if they are on a PC it will download a Windows Media Player file.
Cheers
CHRIS
回答1:
You can check the UserAgent header to tell Mac and PC browsers apart.
回答2:
Yes. It's not foolproof, but can be done. Here's a sample of Javascript detecting your OS. You could simply opt to display a different div
or something similar depending on the result.
回答3:
Here is an example
<html>
<head>
<script type="text/javascript">
function yourOS() {
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("win") != -1) {
return "Windows";
} else if (ua.indexOf("mac") != -1) {
return "Macintosh";
} else if (ua.indexOf("linux") != -1) {
return "Linux";
} else if (ua.indexOf("x11") != -1) {
return "Unix";
} else {
return "Computers";
}
}
</script>
<body>
<h1>Welcome to GiantCo Computers</h2>
<h2>We love
<script type="text/javascript">document.write(yourOS())</script>
<noscript>Computers</noscript>
Users!</h2>
</body>
</html>
from http://www.java2s.com/Code/JavaScript/Development/Getusersoperatingsysteminformation.htm
来源:https://stackoverflow.com/questions/1429058/change-link-destination-based-on-whether-user-has-mac-or-pc