2012-02-06 21 views
13

https://github.com/maranomynet/linkifyJQuery Linkify प्लगइन का उपयोग करते समय, मैं url को कैसे छोटा कर सकता हूं?

मैं इस प्लगइन का उपयोग कर रहा हूं। यह काम करता है, और सबकुछ ठीक है। लेकिन क्या कोई विकल्प है जिसमें मैं प्लग कर सकता हूं ताकि अगर यूआरएल लंबाई "एक्स" से अधिक हो, तो इसे छोटा करें, और "..." जोड़ें?

अभी यूआरएल इतने लंबे हैं।

मुझे डेमो में नोटिस है कि "हैंडल लिंक" कॉलबैक फ़ंक्शन है, लेकिन मैं इसका उपयोग कैसे करूं?

+0

यदि आप लिंक काट लेंगे तो यह एक लिंक के रूप में बेकार होगा लेकिन यदि आप इसे इस तरह प्रदर्शित करना चाहते हैं तो आप इसे लागू करने के लिए कुछ अन्य jQuery प्लगइन को कार्यान्वित कर सकते हैं। – NAVEED

उत्तर

9

आप सही हैं, आप handleLinks कॉलबैक फ़ंक्शन का उपयोग कर सकते हैं। उदाहरण के लिए मैं सरल कार्यात्मक ने लिखा है कि आप की जरूरत:

handleLinks: function (links) { 
    for (var i = 0, cnt = links.length, tmpLink; i < cnt; i++) { 
     tmpLink = links[i].innerHTML; 
     if (tmpLink.length > 10) { 
      links[i].innerHTML = tmpLink.substr(0, 10) + '...'; 
     } 
    } 
} 

यह अगर वे अधिक समय तक 10 अक्षर लिंक ट्रंकेटस। आप अपनी जरूरतों से इस स्क्रिप्ट को संशोधित कर सकते हैं।

+4

यह अनावश्यक हो सकता है, लेकिन मैं 'लिंक [i] .title = tmpLink;' को जोड़ने का लुत्फ उठाउंगा, बस यह सुनिश्चित करने के लिए कि एक ऐसा माध्यम है जिसके द्वारा उपयोगकर्ता पूर्ण यूआरएल देख सकता है (मुझे पता है कि यह स्थिति में दिखाई देता है स्क्रीन के नीचे बार, लेकिन यहां तक ​​कि ...)। –

3

यूआरएल छंटनी के लिए मैं बीच में छोटा करना चुनता हूं, क्योंकि डोमेन और फ़ाइल आमतौर पर निर्देशिका पथ से अधिक महत्वपूर्ण होती है।

एंड्रयू प्लमर की जावास्क्रिप्ट लाइब्रेरी Sugar के GitHub fork से इस प्रश्न के लिए लिया गया और अनुकूलित किया गया।

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' 

नोट::

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; 
} 

तो उसके एवज में स्ट्रिंग एक यूआरएल को छोटा करने का उदाहरण 20 वर्ण लंबा है इस कोड को केवल सबूत पढ़ने और नहीं इकाई का परीक्षण किया गया है। Sugar implementation हालांकि, unit tested रहा है।

संबंधित मुद्दे

 संबंधित मुद्दे