snipt

Ctrl+h for KB shortcuts

JavaScript

Dynamically Inject External js-file.

1
2
3
4
var script = document.createElement('script');
script.src = "http://script-domain.com/script.js";
script.type="text/javascript";
document.getElementsByTagName('head')[0].appendChild(script);
https://snipt.net/embed/11625359311fae935a1d9f9a74782028/
/raw/11625359311fae935a1d9f9a74782028/
11625359311fae935a1d9f9a74782028
js
JavaScript
5
2019-06-24T20:00:24
True
False
False
/api/public/snipt/3284/
dynamically-inject-external-js-file
<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></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">script</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s1">&#39;script&#39;</span><span class="p">);</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">script</span><span class="p">.</span><span class="nx">src</span> <span class="o">=</span> <span class="s2">&quot;http://script-domain.com/script.js&quot;</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="nx">script</span><span class="p">.</span><span class="nx">type</span><span class="o">=</span><span class="s2">&quot;text/javascript&quot;</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a><span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;head&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">].</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">script</span><span class="p">);</span> </span></pre></div> </td></tr></table>
javascript

JavaScript

copy to clipboard

1
2
3
4
5
6
function copyToClipBoard(id)
{
    document.getElementById('holdtext').innerText = document.getElementById(id).innerText;
    Copied = document.getElementById('holdtext').createTextRange();
    Copied.execCommand("Copy");
}
https://snipt.net/embed/38519f215179b47ca24a1e437947a4ed/
/raw/38519f215179b47ca24a1e437947a4ed/
38519f215179b47ca24a1e437947a4ed
js
JavaScript
6
2019-06-26T00:56:24
True
False
False
/api/public/snipt/2969/
copy-to-clipboard-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></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">copyToClipBoard</span><span class="p">(</span><span class="nx">id</span><span class="p">)</span> </span><span id="L-2"><a name="L-2"></a><span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;holdtext&#39;</span><span class="p">).</span><span class="nx">innerText</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="nx">id</span><span class="p">).</span><span class="nx">innerText</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="nx">Copied</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;holdtext&#39;</span><span class="p">).</span><span class="nx">createTextRange</span><span class="p">();</span> </span><span id="L-5"><a name="L-5"></a> <span class="nx">Copied</span><span class="p">.</span><span class="nx">execCommand</span><span class="p">(</span><span class="s2">&quot;Copy&quot;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
clipboard, javascript

JavaScript+Django/Jinja

js svg raphael example

1
2
3
4
5
txt1 = paper.text(30, 30, "Sample text");
txt1.attr({'font-size': "20px"});
txt1.rotate(45);
txt1.node.onmouseover = function () {txt1.animate({rotation: 40}, 500)};
txt1.node.onmouseout = function () {txt1.animate({rotation: 45}, 500)};
https://snipt.net/embed/f0cbaff3e573df0e6264f0a9468bb67d/
/raw/f0cbaff3e573df0e6264f0a9468bb67d/
f0cbaff3e573df0e6264f0a9468bb67d
js+django
JavaScript+Django/Jinja
6
2019-06-24T21:36:05
True
False
False
/api/public/snipt/2929/
js-svg-raphael-example
<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="nx">txt1</span> <span class="o">=</span> <span class="nx">paper</span><span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="mi">30</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="s2">&quot;Sample text&quot;</span><span class="p">);</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">txt1</span><span class="p">.</span><span class="nx">attr</span><span class="p">({</span><span class="s1">&#39;font-size&#39;</span><span class="o">:</span> <span class="s2">&quot;20px&quot;</span><span class="p">});</span> </span><span id="L-3"><a name="L-3"></a><span class="nx">txt1</span><span class="p">.</span><span class="nx">rotate</span><span class="p">(</span><span class="mi">45</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="nx">txt1</span><span class="p">.</span><span class="nx">node</span><span class="p">.</span><span class="nx">onmouseover</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span><span class="nx">txt1</span><span class="p">.</span><span class="nx">animate</span><span class="p">({</span><span class="nx">rotation</span><span class="o">:</span> <span class="mi">40</span><span class="p">},</span> <span class="mi">500</span><span class="p">)};</span> </span><span id="L-5"><a name="L-5"></a><span class="nx">txt1</span><span class="p">.</span><span class="nx">node</span><span class="p">.</span><span class="nx">onmouseout</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span><span class="nx">txt1</span><span class="p">.</span><span class="nx">animate</span><span class="p">({</span><span class="nx">rotation</span><span class="o">:</span> <span class="mi">45</span><span class="p">},</span> <span class="mi">500</span><span class="p">)};</span> </span></pre></div> </td></tr></table>
example, javascript, raphael, svg

JavaScript

Javascript trim

var trimmed = str.replace(/^\s+|\s+$/g, '')
https://snipt.net/embed/c6892b165099e3266cf66f687c9dd31b/
/raw/c6892b165099e3266cf66f687c9dd31b/
c6892b165099e3266cf66f687c9dd31b
js
JavaScript
1
2019-06-23T04:46:06
True
False
False
/api/public/snipt/2897/
javascript-trim
<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="kd">var</span> <span class="nx">trimmed</span> <span class="o">=</span> <span class="nx">str</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\s+|\s+$/g</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> </span></pre></div> </td></tr></table>
javascript, regex, trim

HTML

Add a class to the body with JavaScript

<script type="text/javascript">document.getElementsByTagName('body')[0].className+=' js'</script>
https://snipt.net/embed/6322b8873ff8524160691103fd67bfb4/
/raw/6322b8873ff8524160691103fd67bfb4/
6322b8873ff8524160691103fd67bfb4
html
HTML
1
2019-06-26T04:10:00
True
False
False
/api/public/snipt/2827/
add-a-class-to-the-body-with-javascript
<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="p">&gt;</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">].</span><span class="nx">className</span><span class="o">+=</span><span class="s1">&#39; js&#39;</span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span> </span></pre></div> </td></tr></table>
body, html, javascript

JavaScript

Javascript to set CSS selector for browser

1
2
3
4
5
6
7
8
/*
CSS Browser Selector v0.3.1
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',h=document.getElementsByTagName('html')[0],b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3')?g+' ff3':is('gecko/')?g:/opera(\s|\/)(\d+)/.test(ua)?'opera opera'+RegExp.$2:is('konqueror')?'konqueror':is('chrome')?w+' '+s+' chrome':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);
https://snipt.net/embed/faff121815802a2af83041183587d58f/
/raw/faff121815802a2af83041183587d58f/
faff121815802a2af83041183587d58f
js
JavaScript
8
2019-06-23T22:36:19
True
False
False
/api/public/snipt/2653/
javascript-to-set-css-selector-for-browser
<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="cm">/*</span> </span><span id="L-2"><a name="L-2"></a><span class="cm">CSS Browser Selector v0.3.1</span> </span><span id="L-3"><a name="L-3"></a><span class="cm">Rafael Lima (http://rafael.adm.br)</span> </span><span id="L-4"><a name="L-4"></a><span class="cm">http://rafael.adm.br/css_browser_selector</span> </span><span id="L-5"><a name="L-5"></a><span class="cm">License: http://creativecommons.org/licenses/by/2.5/</span> </span><span id="L-6"><a name="L-6"></a><span class="cm">Contributors: http://rafael.adm.br/css_browser_selector#contributors</span> </span><span id="L-7"><a name="L-7"></a><span class="cm">*/</span> </span><span id="L-8"><a name="L-8"></a><span class="kd">function</span> <span class="nx">css_browser_selector</span><span class="p">(</span><span class="nx">u</span><span class="p">){</span><span class="kd">var</span> <span class="nx">ua</span> <span class="o">=</span> <span class="nx">u</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">(),</span><span class="nx">is</span><span class="o">=</span><span class="kd">function</span><span class="p">(</span><span class="nx">t</span><span class="p">){</span><span class="k">return</span> <span class="nx">ua</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">t</span><span class="p">)</span><span class="o">&gt;-</span><span class="mi">1</span><span class="p">;},</span><span class="nx">g</span><span class="o">=</span><span class="s1">&#39;gecko&#39;</span><span class="p">,</span><span class="nx">w</span><span class="o">=</span><span class="s1">&#39;webkit&#39;</span><span class="p">,</span><span class="nx">s</span><span class="o">=</span><span class="s1">&#39;safari&#39;</span><span class="p">,</span><span class="nx">h</span><span class="o">=</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;html&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">],</span><span class="nx">b</span><span class="o">=</span><span class="p">[(</span><span class="o">!</span><span class="p">(</span><span class="sr">/opera|webtv/i</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">ua</span><span class="p">))</span><span class="o">&amp;&amp;</span><span class="sr">/msie\s(\d)/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">ua</span><span class="p">))</span><span class="o">?</span><span class="p">(</span><span class="s1">&#39;ie ie&#39;</span><span class="o">+</span><span class="nb">RegExp</span><span class="p">.</span><span class="nx">$1</span><span class="p">)</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;firefox/2&#39;</span><span class="p">)</span><span class="o">?</span><span class="nx">g</span><span class="o">+</span><span class="s1">&#39; ff2&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;firefox/3&#39;</span><span class="p">)</span><span class="o">?</span><span class="nx">g</span><span class="o">+</span><span class="s1">&#39; ff3&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;gecko/&#39;</span><span class="p">)</span><span class="o">?</span><span class="nx">g</span><span class="o">:</span><span class="sr">/opera(\s|\/)(\d+)/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">ua</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;opera opera&#39;</span><span class="o">+</span><span class="nb">RegExp</span><span class="p">.</span><span class="nx">$2</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;konqueror&#39;</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;konqueror&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;chrome&#39;</span><span class="p">)</span><span class="o">?</span><span class="nx">w</span><span class="o">+</span><span class="s1">&#39; &#39;</span><span class="o">+</span><span class="nx">s</span><span class="o">+</span><span class="s1">&#39; chrome&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;applewebkit/&#39;</span><span class="p">)</span><span class="o">?</span><span class="nx">w</span><span class="o">+</span><span class="s1">&#39; &#39;</span><span class="o">+</span><span class="nx">s</span><span class="o">+</span><span class="p">(</span><span class="sr">/version\/(\d+)/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">ua</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39; &#39;</span><span class="o">+</span><span class="nx">s</span><span class="o">+</span><span class="nb">RegExp</span><span class="p">.</span><span class="nx">$1</span><span class="o">:</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;mozilla/&#39;</span><span class="p">)</span><span class="o">?</span><span class="nx">g</span><span class="o">:</span><span class="s1">&#39;&#39;</span><span class="p">,</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;j2me&#39;</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;mobile&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;iphone&#39;</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;iphone&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;ipod&#39;</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;ipod&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;mac&#39;</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;mac&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;darwin&#39;</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;mac&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;webtv&#39;</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;webtv&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;win&#39;</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;win&#39;</span><span class="o">:</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;freebsd&#39;</span><span class="p">)</span><span class="o">?</span><span class="s1">&#39;freebsd&#39;</span><span class="o">:</span><span class="p">(</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;x11&#39;</span><span class="p">)</span><span class="o">||</span><span class="nx">is</span><span class="p">(</span><span class="s1">&#39;linux&#39;</span><span class="p">))</span><span class="o">?</span><span class="s1">&#39;linux&#39;</span><span class="o">:</span><span class="s1">&#39;&#39;</span><span class="p">,</span><span class="s1">&#39;js&#39;</span><span class="p">];</span> <span class="nx">c</span> <span class="o">=</span> <span class="nx">b</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">);</span> <span class="nx">h</span><span class="p">.</span><span class="nx">className</span> <span class="o">+=</span> <span class="s1">&#39; &#39;</span><span class="o">+</span><span class="nx">c</span><span class="p">;</span> <span class="k">return</span> <span class="nx">c</span><span class="p">;};</span> <span class="nx">css_browser_selector</span><span class="p">(</span><span class="nx">navigator</span><span class="p">.</span><span class="nx">userAgent</span><span class="p">);</span> </span></pre></div> </td></tr></table>
browser, css, javascript, selector