Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change jQuery code to hide .jpg at the end of the url?

Could someone help me with this short piece of code? Right now when I click thumbnail image on my site it creates url in the address bar. But url has .jpg at the end. How should I modify the code so that it won't show .jpg at the end of the url?

This piece of code also opens Colorbox image automatically if user enters site with url like www.domain.com/#image.jpg so naturally the change to the code should affect also to that.

Thanks!

jQuery(function() {
    var id, group;

    group = jQuery("a[rel='lightbox[63]']").colorbox({ 
        onComplete: function() {
            window.location.hash = (this.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1];
        }, 
        onClosed: function() {
            location.hash = '';
        }
    });

    id = location.hash.replace(/^\#/, '');
    group.filter('[href$="'+id+'"]').eq(0).click();
});
like image 745
Nitrok Avatar asked Jan 27 '13 13:01

Nitrok


1 Answers

Replace this:

window.location.hash = (this.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1];

With this:

// Get the image URL
with_ext = (this.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1];
// Get the image url without the extension
without_ext =   with_ext.substring(0, with_ext.lastIndexOf(".")));
// Redirect
window.location.hash = without_ext;

Explanation

  • my_str.lastIndexOf(".") returns the position of the last . character in the string my_str
  • my_str.substring(n, m), where n and m are numbers returns the characters in a my_str beginning at n through m characters - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/substr

Example

http://jsbin.com/uqufev/1/edit

like image 101
Nabil Kadimi Avatar answered Oct 02 '22 00:10

Nabil Kadimi