Search code examples
jqueryautoloadfile-renameslimbox

Geting a file name without the ".jpg" part


Ok I am using slimbox2 and want the user to be able to save a Hi-Res version of the image. Adding this to the .js file allows the EXACT same image to be saved with a link in the title:

    // AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED) 
    jQuery(function($) {
    $("a[rel^='lightbox']").slimbox({/* Put custom options here */}, function(el) {
    return [el.href, el.title + '<br /><a href="' + el.href + '">
    Download this image</a>'];
    }, function(el) {
            return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
    });

However I want the user to be able to save a Hi-Res version with just "lg" added to the name.

for instance instead of downloading "image1.jpg" which is displayed, it downloads "image1lg.jpg"

The code above uses el.href to load the image into the link but if I use + 'lg' it adds that BEHIND the .jpg, so I get "image1.jpglg" which won't work.

Is there a way to grave the el.href and then subtract just the ".jpg" from the name, then add the "lg.jpg" to the file name?

Hope that makes sense, thanks for the time.


Solution

  • If lg is always the last part of the filename before the extension, try this:

    return [el.href, el.title + '<br /><a href="' + el.href.replace(/\.jpg$/i, 'lg.jpg') + '">Download this image</a>'];