skip to Main Content

I have this code in my html source

<body>
 <a class="android">back to home page</a>
</body>

how can i use inline js/jquery code to say if android phone click "back to home page" button href= "x.com" else href= "z.com"
I want to use inline source js/jquery in my html document

2

Answers


  1. Chosen as BEST ANSWER
    <body>
      <script type="text/javascript">
        var isAndroid = / Android/i.test(navigator.userAgent.toLowerCase());
     
        if (isAndroid) {
          document.getElementById("abc").href = "https://stackoverflow.com/;
        } else {
          document.getElementById("abc").href = "https://link2";
        }
      </script>
    </body>
    

  2. You can use the following code in

    <a class="android" id="android" >back to home page</a>

    let anchor = document.getElementById("android");
    if (screen.width <= 640) { 
        anchor.href="link"
     }
    else{
        anchor.href="link2"
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search