使用os检测自动下载

时间:2018-10-02 09:03:47

标签: javascript html download

165/5000 我想用JavaScript创建一个网页,该网页可以自动检测用户拥有的操作系​​统。然后下载正确的安装文件。

这是我的代码

html

<!DOCTYPE html>
<html>
<body>
    <iframe id="my_iframe" style="display:none;"></iframe>
     <script src="os.js"></script> 
    </body>
</html>

javascript

var OSName = "Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

if (OSName =  "Windows"){
    window.location = "https://test.leonwolvekamp.nl/TeamViewer_Setup.exe"


    };

if (OSName = "MacOS"){
     window.location = "https://test.leonwolvekamp.nl/TeamViewer.dmg"

    }


    };

但是下载不会开始

1 个答案:

答案 0 :(得分:0)

Your if statements only have one =. They need two. Also, you had an extra closing bracket. Try this code:

var OSName = "Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

if (OSName ==  "Windows"){
    window.location = "https://test.leonwolvekamp.nl/TeamViewer_Setup.exe"
    };

if (OSName == "MacOS"){
     window.location = "https://test.leonwolvekamp.nl/TeamViewer.dmg"
    };