简体   繁体   中英

HTML checking if using android phone

My website for a class project requires you to check if you are on an android platform, such as a phone. If you are it should make a button visible, otherwise this button is never visible. and the exact same thing with a computer user, if you are on a mac or pc runnning linux or windows or macOSX (doesn't really mater), then a button will become visible but only that button, and not the android button.

I dont truly know how to do this, all I know how to do is check if you're on an android device using this code:

<script>
    function goBack() {
        window.history.back()
    }
    var ua = navigator.userAgent.toLowerCase();
    var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
    if(isAndroid) {
        <button onclick="goBack()">You're on an Android device!</button>
    }
</script>

You can't mix HTML with javascript how you're doing it, however, you can apply that javascript to display the correct buttons. Check out this jsFiddle. You can do something like this:

http://jsfiddle.net/4gr3K/1/

<html>
  <script>
    function goBack() {
      window.history.back()
    }
    var ua = navigator.userAgent.toLowerCase();
    var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
    if(isAndroid) {
      document.getElementById('android-button').style.display = 'inline';
    } else {
      // is a computer
      document.getElementById('computer-button').style.display = 'inline';
    }
  </script>
  <body>
    <input type="button" id="android-button" style="display: none" value="android"/>
    <input type="button" id="computer-button" style="display: none" value= "computer"/>
  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM