1

Many anchor tags on my website have Windows ".exe" file link in its href attribute.

If that anchor tag is clicked on Mobile or Mac OS, I want to popup a message: "Sorry only available on Windows".

Any anchor tag whose href ends with ".exe" matches the condition. How can I select such anchor tags in jQuery?

For example:

<a href="http://www.example.com/downloads/abc.exe"></a>

should be selected by jQuery because it ends with ".exe"

But

<a href="http://www.example.com/downloads/abc.jpg"></a>

should "NOT" be selected by jQuery because it does not end with ".exe" in it.

I have the following code:

if (/Mobi|Android/i.test(navigator.userAgent))
{
 jQuery("a[href*='.exe']").attr("href","#popmake-1117");
}

But it detects .exe anywhere in the href, and not in the end. Also, it works only on mobiles and not on Mac.

Computer User
  • 2,839
  • 4
  • 47
  • 69

4 Answers4

1

You can use indexOf() to check if it contains specify string

$(window).ready(function(){
  $("a").on("click",function(){
     var href = $(this).attr("href");
     if(href.indexOf("example.com/downloads") > -1 && href.indexOf(".exe")> - 1){
       alert("Can download");
     }else{
       alert("Sorry only available on Windows");
     }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<a href="http://www.example.com/downloads/abc.exe">Download exe</a>
<a href="http://www.example.com/downloads/abc.apk">Download apk</a>
flyingfox
  • 13,414
  • 3
  • 24
  • 39
1

look for index of two values.

var link=$('a').attr('href'); 
if(link.indexOf('example.com/downloads')>-1 && 
 link.indexOf('.exe')==link.length-4){
//do something
 }
Ercan Peker
  • 1,662
  • 10
  • 17
1

Update: While the .filter() works out for more complex conditions, there is a valid CSS modifier ([attribute$="value"]) for ends with:

if (/Mobi|Android/i.test(navigator.userAgent)) {
  jQuery("a[href$='.exe']").attr("href","#popmake-1117");
}

Initial answer

This is how I'd do it, using .filter():

let exes = $('a[href]').filter(function(){
  return $(this).attr('href').indexOf('.exe') > -1
});

// let's test it

exes.css({
  border: '1px solid red'
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<a href="#something.exe">has exe</a>
<a href="#something">no exe</a>

To check if .exe is ending the string, you can use substring:

let exes = $('a[href]').filter(function(){
  return $(this).attr('href').substr($(this).attr('href').length - 4) === '.exe'
});

// let's test it

exes.css({
  border: '1px solid red'
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<a href="#something.exe">has exe</a>
<a href="#something">no exe</a>
<a href="#something.exellent">exe not at the end of it</a>
tao
  • 82,996
  • 16
  • 114
  • 150
  • Can I check if href specifically ends with ".exe" ? and not anywhere in it? – Computer User Oct 06 '18 at 14:41
  • 1
    @Comp yes, you can do `return $(this).attr('href').substr($(this).attr('href').length - 4) === '.exe'` – tao Oct 06 '18 at 14:43
  • 1
    @Comp, updated answer with another method, which appears to be what you should use. There's a valid CSS modified for *"ends with"*. `$('a[href$=".exe"]')` – tao Oct 06 '18 at 15:03
1

Use a combination of navigator.platform and indexOf

$(document).ready(function() {
  $("a").click(function(e) {
    var href = $(this).attr("href");

    if (href.indexOf(".exe") > -1) {
      console.log(navigator.platform);

      if (navigator.platform.indexOf("Win") > -1) {
        alert("You're using windows!");
        //proceed

      } else {
        alert("Only available in Windows!");
        e.preventDefault();
      }
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="www.test/test.exe">Test.exe</a>
<a href="https://www.google.com">Google</a>
Jerdine Sabio
  • 5,688
  • 2
  • 11
  • 23