snipt

Ctrl+h for KB shortcuts

JavaScript

The quicker way to iterate over an array. Avoids repeated calls of myArray.length.

1
2
3
for(var i=0,len=myArray.length; i<len; i++){
   doItQuick( myArray[i] );
}
https://snipt.net/embed/c69fe58cefd19ad285dc8d557e89eae1/
/raw/c69fe58cefd19ad285dc8d557e89eae1/
c69fe58cefd19ad285dc8d557e89eae1
js
JavaScript
4
2019-06-24T20:00:00
True
False
False
/api/public/snipt/6420/
the-quicker-way-to-iterate-over-an-array-avoids-repeated-calls-of-myarraylength
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">i</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="nx">len</span><span class="o">=</span><span class="nx">myArray</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">&lt;</span><span class="nx">len</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">){</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">doItQuick</span><span class="p">(</span> <span class="nx">myArray</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
array, for-loop, js

JavaScript

HTML 5 enabling script

1
2
3
4
5
6
7
<!--[if IE]>
<script src="html5.js" type="text/javascript"></script>
<![endif]-->


// See http://snipplr.com/view/14380/html5-enabling-script/
(function(){if(!/*@[email protected]*/0)return;var e = "abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,mark,menu,meter,nav,output,progress,section,time,video".split(','),i=e.length;while (i--){document.createElement(e[i])}})()
https://snipt.net/embed/15109b27ca74006edd85420ed9024280/
/raw/15109b27ca74006edd85420ed9024280/
15109b27ca74006edd85420ed9024280
js
JavaScript
7
2019-06-26T07:20:43
True
False
False
/api/public/snipt/6288/
html5-enablig-script
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c">&lt;!--</span><span class="p">[</span><span class="k">if</span> <span class="nx">IE</span><span class="p">]</span><span class="o">&gt;</span> </span><span id="L-2"><a name="L-2"></a><span class="o">&lt;</span><span class="nx">script</span> <span class="nx">src</span><span class="o">=</span><span class="s2">&quot;html5.js&quot;</span> <span class="nx">type</span><span class="o">=</span><span class="s2">&quot;text/javascript&quot;</span><span class="o">&gt;&lt;</span><span class="err">/script&gt;</span> </span><span id="L-3"><a name="L-3"></a><span class="o">&lt;!</span><span class="p">[</span><span class="nx">endif</span><span class="p">]</span><span class="o">--&gt;</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="c1">// See http://snipplr.com/view/14380/html5-enabling-script/</span> </span><span id="L-7"><a name="L-7"></a><span class="p">(</span><span class="kd">function</span><span class="p">(){</span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="cm">/*@[email protected]*/</span><span class="mi">0</span><span class="p">)</span><span class="k">return</span><span class="p">;</span><span class="kd">var</span> <span class="nx">e</span> <span class="o">=</span> <span class="s2">&quot;abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,mark,menu,meter,nav,output,progress,section,time,video&quot;</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">),</span><span class="nx">i</span><span class="o">=</span><span class="nx">e</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="k">while</span> <span class="p">(</span><span class="nx">i</span><span class="o">--</span><span class="p">){</span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="nx">e</span><span class="p">[</span><span class="nx">i</span><span class="p">])}})()</span> </span></pre></div> </td></tr></table>
html, ie, js

JavaScript

not funny comments 1

var aIDs = {...};
//if(aIDs.isInfected()){ die(); }
https://snipt.net/embed/1609d5a9dedb03af769751861410622c/
/raw/1609d5a9dedb03af769751861410622c/
1609d5a9dedb03af769751861410622c
js
JavaScript
2
2019-06-25T06:26:15
True
False
False
/api/public/snipt/6259/
funny-comments-1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">var</span> <span class="nx">aIDs</span> <span class="o">=</span> <span class="p">{...};</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">//if(aIDs.isInfected()){ die(); }</span> </span></pre></div> </td></tr></table>
"no fun", "not funny", aids, comment, js

HTML

Update IE6 Script

<!--[if IE 6]><script type="text/javascript">if(typeof jQuery == 'undefined'){ document.write("<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"></"+"script>"); var __noconflict = true; } var IE6UPDATE_OPTIONS = {icons_path: "http://static.ie6update.com/hosted/ie6update/images/"}</script>
  <script type="text/javascript" src="http://static.ie6update.com/hosted/ie6update/ie6update.js"></script><![endif]-->
https://snipt.net/embed/0ff874337308f613cf0f36482c261c61/
/raw/0ff874337308f613cf0f36482c261c61/
0ff874337308f613cf0f36482c261c61
html
HTML
2
2019-06-12T07:59:30
True
False
False
/api/public/snipt/6175/
update-ie6-script
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c">&lt;!--[if IE 6]&gt;&lt;script type=&quot;text/javascript&quot;&gt;if(typeof jQuery == &#39;undefined&#39;){ document.write(&quot;&lt;script type=\&quot;text/javascript\&quot; src=\&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\&quot;&gt;&lt;/&quot;+&quot;script&gt;&quot;); var __noconflict = true; } var IE6UPDATE_OPTIONS = {icons_path: &quot;http://static.ie6update.com/hosted/ie6update/images/&quot;}&lt;/script&gt;</span> </span><span id="L-2"><a name="L-2"></a><span class="c"> &lt;script type=&quot;text/javascript&quot; src=&quot;http://static.ie6update.com/hosted/ie6update/ie6update.js&quot;&gt;&lt;/script&gt;&lt;![endif]--&gt;</span> </span></pre></div> </td></tr></table>
ie, javascript, js, script

JavaScript

lazy create Element + Prototype IE fix

1
2
3
function $N(sTag){
    return $(document.createElement(sTag));
}
https://snipt.net/embed/96d3465d1b9c3f750a37ac1d214d4e5f/
/raw/96d3465d1b9c3f750a37ac1d214d4e5f/
96d3465d1b9c3f750a37ac1d214d4e5f
js
JavaScript
3
2019-06-26T02:45:43
True
False
False
/api/public/snipt/6063/
lazy-create-element-prototype-ie-fix
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">function</span> <span class="nx">$N</span><span class="p">(</span><span class="nx">sTag</span><span class="p">){</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">return</span> <span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="nx">sTag</span><span class="p">));</span> </span><span id="L-3"><a name="L-3"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
js, prototype

JavaScript

jQuery Unify Height

/* Make the height of all the innerDivs the same height based on the tallest one. */
function unifyHeights()   {    var maxHeight = 0;    $('div#OuterContainer').children('div').each(function(){      var height = $(this).outerHeight();      //alert(height);      if ( height > maxHeight ) { maxHeight = height; }    });    $('div.innerDivs').css('height', maxHeight);  }
https://snipt.net/embed/3fbb14b5362b590300e4494d621c311d/
/raw/3fbb14b5362b590300e4494d621c311d/
3fbb14b5362b590300e4494d621c311d
js
JavaScript
2
2019-06-23T22:32:50
True
False
False
/api/public/snipt/5967/
jquery-unify-height
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cm">/* Make the height of all the innerDivs the same height based on the tallest one. */</span> </span><span id="L-2"><a name="L-2"></a><span class="kd">function</span> <span class="nx">unifyHeights</span><span class="p">()</span> <span class="p">{</span> <span class="kd">var</span> <span class="nx">maxHeight</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;div#OuterContainer&#39;</span><span class="p">).</span><span class="nx">children</span><span class="p">(</span><span class="s1">&#39;div&#39;</span><span class="p">).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span> <span class="kd">var</span> <span class="nx">height</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">outerHeight</span><span class="p">();</span> <span class="c1">//alert(height); if ( height &gt; maxHeight ) { maxHeight = height; } }); $(&#39;div.innerDivs&#39;).css(&#39;height&#39;, maxHeight); }</span> </span></pre></div> </td></tr></table>
javascript, js

JavaScript

jQuery Open Window Link

1
2
3
4
5
6
$(function(){
$('a.new').click(function(){
window.open(this.href);
return false;
});
});
https://snipt.net/embed/0ca0c42e0b554cd62505016f362f919c/
/raw/0ca0c42e0b554cd62505016f362f919c/
0ca0c42e0b554cd62505016f362f919c
js
JavaScript
6
2019-06-25T04:02:55
True
False
False
/api/public/snipt/5966/
jquery-open-window-link
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nx">$</span><span class="p">(</span><span class="kd">function</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="s1">&#39;a.new&#39;</span><span class="p">).</span><span class="nx">click</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="nb">window</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">href</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="k">return</span> <span class="kc">false</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="p">});</span> </span><span id="L-6"><a name="L-6"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
javascript, js

HTML

jQuery

<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("jquery", "1");</script>
https://snipt.net/embed/c9e68e81f67f3a2cf547713d3de7f82a/
/raw/c9e68e81f67f3a2cf547713d3de7f82a/
c9e68e81f67f3a2cf547713d3de7f82a
html
HTML
1
2019-06-17T15:54:49
True
False
False
/api/public/snipt/5965/
jquery-1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;http://www.google.com/jsapi&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text/javascript&quot;</span><span class="p">&gt;</span><span class="nx">google</span><span class="p">.</span><span class="nx">load</span><span class="p">(</span><span class="s2">&quot;jquery&quot;</span><span class="p">,</span> <span class="s2">&quot;1&quot;</span><span class="p">);&lt;/</span><span class="nt">script</span><span class="p">&gt;</span> </span></pre></div> </td></tr></table>
javascript, js, library