snipt

Ctrl+h for KB shortcuts

JavaScript

Limit string length/trim text with jQuery

1
2
3
4
5
6
7
8
jQuery(document).ready(function($){
    $("h3").each(function(){
	        if ($(this).text().length > 30) {
	            $(this).text($(this).text().substr(0, 27));
	            $(this).append('...');
	        }
	    });
});
https://snipt.net/embed/7658a6489591d5bd71006312b7ef1631/
/raw/7658a6489591d5bd71006312b7ef1631/
7658a6489591d5bd71006312b7ef1631
js
JavaScript
8
2016-12-28T14:15:55
True
False
True
Jul 10, 2012 at 01:50 PM
/api/public/snipt/43724/
limit-string-lengthtrim-text-with-jquery-1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nx">jQuery</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">$</span><span class="p">){</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">$</span><span class="p">(</span><span class="s2">&quot;h3&quot;</span><span class="p">).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">text</span><span class="p">().</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">30</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-4"><a name="L-4"></a> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">text</span><span class="p">().</span><span class="nx">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">27</span><span class="p">));</span> </span><span id="L-5"><a name="L-5"></a> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="s1">&#39;...&#39;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a> <span class="p">}</span> </span><span id="L-7"><a name="L-7"></a> <span class="p">});</span> </span><span id="L-8"><a name="L-8"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
jquery

JavaScript

Quick Caching Images

1
2
3
4
5
//cache the user img
$('img').hide().appendTo('body').one('load', function() {
   console.log('Image: '+$(this).attr('src')+' is cached...');
   $(this).remove();
});
https://snipt.net/embed/7d8d34d8c467aeabd8ca5e4d3e8425d5/
/raw/7d8d34d8c467aeabd8ca5e4d3e8425d5/
7d8d34d8c467aeabd8ca5e4d3e8425d5
js
JavaScript
5
2016-12-28T02:20:14
True
False
True
Jun 29, 2012 at 02:31 AM
/api/public/snipt/43172/
quick-caching-images
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">//cache the user img</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;img&#39;</span><span class="p">).</span><span class="nx">hide</span><span class="p">().</span><span class="nx">appendTo</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">).</span><span class="nx">one</span><span class="p">(</span><span class="s1">&#39;load&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Image: &#39;</span><span class="o">+</span><span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;src&#39;</span><span class="p">)</span><span class="o">+</span><span class="s1">&#39; is cached...&#39;</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">remove</span><span class="p">();</span> </span><span id="L-5"><a name="L-5"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
jquery