Per l'URL troncato, ho scelto di abbreviare nel mezzo, in quanto il dominio e il file sono in genere più importanti del percorso della directory.
Scattata e adattata per questa domanda dal mio GitHub fork della Libreria JavaScript di Andrew Plummer Sugar.
String.prototype.shorten = function(length, position, countSplitter, splitter) {
if (this.length < 1 && length < 1) return String(this);
if (!(typeof(splitter) === 'string')) splitter = '...';
if (!(typeof(countSplitter) === 'boolean')) countSplitter = true;
var balance = (countSplitter) ? splitter.length : 0;
if (length <= balance || this.length <= length) return String(this);
// Perform shortening
var shortened, beforeSplitter, afterSplitter;
if (position == 'left') {
afterSplitter = this.substring(this.length - length + balance, this.length - 1);
shortened = splitter + afterSplitter;
} else if (position == 'right') {
beforeSplitter = this.substring(0, length - balance);
shortened = beforeSplitter + splitter;
} else {
beforeSplitter = this.substring(0, Math.ceil((length/2) - (balance/2)));
afterSplitter = this.substring(this.length - Math.floor((length/2) - (balance/2)), this.length);
shortened = beforeSplitter + splitter + afterSplitter;
}
return shortened;
}
Esempio di accorciare un URL così la stringa risultante è 20 caratteri:
var toShorten = 'http://stackoverflow.com/questions/9156458/when-using-jquery-linkify-plugin-how-do-i-truncate-the-url';
var shortened = toShorten.shorten(20); // Output: 'http://st...-the-url'
Nota: questo codice è stato solo riletti e non unità testata. Il Sugar implementation è stato unit tested, tuttavia.
fonte
2012-02-09 15:48:50
Se si taglia il collegamento, sarà inutile come collegamento ma se si desidera visualizzarlo in questo modo è possibile implementare altri plugin jQuery per implementarlo. – NAVEED