Redirect to a new windows, with JavaScript, with a onclick on <a></a>

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/store/apps/details?id=com.gecose.appseguros360.irgmediassegur";
    if (OSName == 0) OSName = "apps.apple.com/es/app/irg-mediassegur/id1509456102";


</script>

<a href="https://www.mediassegur.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. 🙂

130 thoughts on “Redirect to a new windows, with JavaScript, with a onclick on <a></a>”

Leave a Comment