1

I am trying to traverse and find links with a specific extension (*.ashx) so that I can open the link in a new tab. (Sitefinity does not allow target="_blank").

I can find the tags using jQuery, but I need to then filter it more so that when I click on an tag with an extension of .ashx, I can open this in a new window.

Something like this

<a href="anniversary.sflb.ashx"> Anniversary </a> 

Many thanks, James

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
Sixfoot Studio
  • 2,951
  • 7
  • 26
  • 35

3 Answers3

5

It would be better to use the attribute contains selector

$("a[href*='.ashx']").each(
  function() {
  //Do stuff
});

see http://jsfiddle.net/graham/Fa6kV/

graham
  • 946
  • 5
  • 16
2

The two answers provided so far (graham's and Steve's) are both potentially inaccurate: graham's will sometimes match links that have ashx somewhere in the string, but not necessarily at the end of the string -- bashxml.php would match, for instance. Steve's, meanwhile, will not match if there's a query string, as he notes.

You can get round this by doing the filtering yourself:

$('a').each(function() {
    if (this.pathname.substr(-5) === '.ashx') { // if the last 5 characters of the pathname are .ashx
        // do your processing here
    }
});

See documentation on the Location object.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

You could use the attribute ends with selector:

$("a[href$='.ashx']")

Note: this wouldn't work if your links have a query string or some other suffix in the href attribute

Steve Greatrex
  • 15,789
  • 5
  • 59
  • 73