URL'yi kesmek için Etki alanı ve dosya genellikle dizin yolundan daha önemli olduğundan, kısaltmayı seçiyorum.
Bu soru için alınan ve uyarlanmış olan Andrew Plummer'ın GitHub fork adresinden Sugar.
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'
Not: Sonuç dizesini öylesine bir Url kısaltma
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;
}
Örnek uzunluğunda 20 karakter bu kod yalnızca test kanıt okuma ve birim olmuştur. Bununla birlikte, Sugar implementation, unit tested olmuştur.
Bağlantıyı kesecekseniz bağlantı olarak işe yaramıyor, ancak bu şekilde görüntülemek istiyorsanız, bunu uygulamak için başka bir jQuery eklentisi uygulayabilirsiniz. – NAVEED