1

I was making this code to insert it into my website, to detect the OS from the devices, and depending which OS they have, redirect to different pages. Also, if I upload the code to my website, the href doesn't work really well, and it searches https://example.com/thehref instead of the href alone.

This is what I have been doing, to solve the problem.

<script>

  var OSName = "Unknown OS";
    if (navigator.userAgent.indexOf("Win") != -1) OSName = 1;
    if (navigator.userAgent.indexOf("Mac") != -1) OSName = 0;
    if (navigator.userAgent.indexOf("Linux") != -1) OSName = 1;
    if (navigator.userAgent.indexOf("Android") != -1) OSName = 1;
    if (navigator.userAgent.indexOf("like Mac") != -1) OSName = 0;

    if (OSName == 1) OSName = "play.google.com";
    if (OSName == 0) OSName = "apps.apple.com";


</script>

<a href="https://www.google.com" onclick="location.href=this.href+OSName;return false;"> <p>Click<br> at me!!!</p> </a>

And my idea is that every time someones click on it, it redirects them to another page, I have been searching, and it seems that onclik and target="_blank" don't work well together.

If someone knows how to solve the problem, or has an idea, feel free to comment.

Thx you all. :)

2 Answers2

1

Please try this one.

<script>
  var OSName = "Unknown OS";

  if (navigator.userAgent.indexOf("Win") != -1) OSName = 1;
  if (navigator.userAgent.indexOf("Mac") != -1) OSName = 0;
  if (navigator.userAgent.indexOf("Linux") != -1) OSName = 1;
  if (navigator.userAgent.indexOf("Android") != -1) OSName = 1;
  if (navigator.userAgent.indexOf("like Mac") != -1) OSName = 0;

  if (OSName == 1) OSName = "page1";
  if (OSName == 0) OSName = "page2";

  function openPage() {
    window.open(window.location.href + '/' + OSName);
    return false;
  }
</script>

<a href="https://" onclick="openPage()"> <p>Click<br> at me!!!</p> </a>
Louis
  • 61
  • 5
  • Hey, thanks for giving me another option, but with the first one, it works fine. You just need to keep in mind, that if you don't put anything in the href camp, it will block the first page. – Coding 4fun Jan 27 '21 at 09:17
  • It has to be a valid link, inside the href. – Coding 4fun Jan 27 '21 at 09:20
1

You can create another <a> tag and call click() in JS

    var OSName = "Unknown OS";
    if (navigator.userAgent.indexOf("Win") != -1) OSName = 1;
    if (navigator.userAgent.indexOf("Mac") != -1) OSName = 0;
    if (navigator.userAgent.indexOf("Linux") != -1) OSName = 1;
    if (navigator.userAgent.indexOf("Android") != -1) OSName = 1;
    if (navigator.userAgent.indexOf("like Mac") != -1) OSName = 0;
  
    if (OSName == 1) OSName = "play.google.com/store/apps/details?id=com.gecose.appseguros360.irgmediassegur";
    if (OSName == 0) OSName = "apps.apple.com/es/app/irg-mediassegur/id1509456102";
  
    function onClick() {
        let a = document.createElement('a')
        a.setAttribute('href', 'https://'+OSName)
        a.setAttribute('target', '_blank')
        a.click()
    }
</script>
<a onclick="onClick()" > <p>Click<br> at me!!!</p> </a>
kswang
  • 160
  • 9