0

This jQuery code works half the time depending on browser. Is there a better way to write this replace. Change and href link with .zip_ to .zip

jQuery("a[href*='.zip_']").each(function() {
  jQuery(this).attr("href", jQuery(this).attr("href").replace(/\.zip_/g, ".zip"));
});
acctman
  • 4,229
  • 30
  • 98
  • 142

1 Answers1

0

Your solution is fine but it could be broken up a little bit to make it easier to read. I would personally use:

jQuery("a[href*='.zip_']").each(function() {
  const attr = jQuery(this).attr("href").split(".");
  attr[attr.length - 1] = ".zip";
  jQuery(this).attr("href", attr.join(''));
});

This should work as Mozilla suggests split works in all browsers. Also make sure you are using this in your $(document).ready(). This also makes the assumption .zip is at the end of the href.

Rawley Fowler
  • 1,366
  • 7
  • 15