Вопрос

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.

Это было полезно?

Решение

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>'];

Другие советы

I would use javascript's replace function. You can match a string or regex expression and replace it with whatever you want, in this case matching '.jpg' and replacing it with 'lg.jpg'.

jQuery(function($) {
    $("a[rel^='lightbox']").slimbox({/* Put custom options here */}, function(el) {
        return [el.href, el.title + '<br /><a href="' + el.href.replace('.jpg', 'lg.jpg') + '"> Download this image</a>'];
    }, function(el) {
        return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
});

This answer is not an be-all end-all answer but it does solve your specific question.

This script will essentially fail if the filename has more than one period

Yes there is a Javascript split() function:

// The file
var string = 'filename.jpg'

// Split on periods
var parts = string.split('.');

// parts[0] = filename
// parts[1] = jpg

// Bring the string back together and add an 'lg' where needed
var new_string = parts[0]+'lg.'+parts[1];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top