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.

0

4 Answers 4

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>

Sign up to request clarification or add additional context in comments.

3 Comments

Can I check if href specifically ends with ".exe" ? and not anywhere in it?
@Comp yes, you can do return $(this).attr('href').substr($(this).attr('href').length - 4) === '.exe'
@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"]')
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>

3 Comments

Can I check if href specifically ends with ".exe" ? and not anywhere in it?
yes you can check if index of '.exe' is equal length of href minus length of '.exe' which is 4. i edited my answer, accordingly.
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
 }

1 Comment

Can I check if href specifically ends with ".exe" ? and not anywhere in it?
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.