<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CSS-TricksCSS-Tricks</title>
	<atom:link href="http://css-tricks.com/javascript-snippets-feed/" rel="self" type="application/rss+xml" />
	<link>http://css-tricks.com</link>
	<description>Tips, Tricks, and Techniques on using Cascading Style Sheets.</description>
	<lastBuildDate>Wed, 19 Jun 2013 02:19:46 +0000</lastBuildDate>
	<language></language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>

	<generator>http://wordpress.org/?v=3.5.1</generator>

	
		<item>
			<title>Test if dragenter/dragover Event Contains Files</title>
			<link>http://css-tricks.com/snippets/javascript/test-if-dragenterdragover-event-contains-files/</link>
			<comments>http://css-tricks.com/snippets/javascript/test-if-dragenterdragover-event-contains-files/#comments</comments>
			<pubDate>Tue, 26 Mar 2013 00:20:28 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=20786</guid>

			<description><![CDATA[<p>HTML5 drag and drop is great for handling file uploads. But if that's the only thing you are using it for, it's nice to know if any particular <code>dragenter</code> or <code>dragover</code> event actually has files. Unlike, for example, just the dragging of some selected text.</p>
<p>Send the event object to this function and it will return the truth (assuming you are in a browser that <a href="http://caniuse.com/#feat=dragndrop">supports all this</a>):</p>
<code class="language-javascript">function containsFiles(event) {

    if (event.dataTransfer.types) {
        for (var i = 0; &#8230;</code>]]></description>

			<content:encoded><![CDATA[<p>HTML5 drag and drop is great for handling file uploads. But if that's the only thing you are using it for, it's nice to know if any particular <code>dragenter</code> or <code>dragover</code> event actually has files. Unlike, for example, just the dragging of some selected text.</p>
<p>Send the event object to this function and it will return the truth (assuming you are in a browser that <a href="http://caniuse.com/#feat=dragndrop">supports all this</a>):</p>
<pre rel="JavaScript"><code class="language-javascript">function containsFiles(event) {

    if (event.dataTransfer.types) {
        for (var i = 0; i &lt; event.dataTransfer.types.length; i++) {
            if (event.dataTransfer.types[i] == "Files") {
                return true;
            }
        }
    }
    
    return false;

}</code></pre>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/javascript/test-if-dragenterdragover-event-contains-files/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>

						
		</item>

	
		<item>
			<title>Fix IE 10 on Windows Phone 8 Viewport</title>
			<link>http://css-tricks.com/snippets/javascript/fix-ie-10-on-windows-phone-8-viewport/</link>
			<comments>http://css-tricks.com/snippets/javascript/fix-ie-10-on-windows-phone-8-viewport/#comments</comments>
			<pubDate>Sun, 27 Jan 2013 17:02:36 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=19994</guid>

			<description><![CDATA[<code class="language-javascript">(function() {
  if (navigator.userAgent.match(/IEMobile\/10\.0/)) {
    var msViewportStyle = document.createElement("style");
    msViewportStyle.appendChild(
      document.createTextNode("@-ms-viewport{width:auto!important}")
    );
    document.getElementsByTagName("head")[0].appendChild(msViewportStyle);
  }
})();</code>
<h4>Quick backstory</h4>
<p>For IE 10 (desktop) to work in it's new "snap mode" you need to use this:</p>
<code class="language-css">@-ms-viewport {
  width: device-width;
}</code>
<p>But that screws up some Windows Phone 8 phones, overriding the meta viewport tag and rendering as far too large on small screens. So the answer, for now, is this gnarly device detection/injection script.</p>
<h4>Longer backstory</h4>
<ul>
<li>Matt Stow: <a href="http://mattstow.com/responsive-design-in-ie10-on-windows-phone-8.html">Responsive Design in IE10 </a></li>&#8230;</ul>]]></description>

			<content:encoded><![CDATA[<pre rel="JavaScript"><code class="language-javascript">(function() {
  if (navigator.userAgent.match(/IEMobile\/10\.0/)) {
    var msViewportStyle = document.createElement("style");
    msViewportStyle.appendChild(
      document.createTextNode("@-ms-viewport{width:auto!important}")
    );
    document.getElementsByTagName("head")[0].appendChild(msViewportStyle);
  }
})();</code></pre>
<h4>Quick backstory</h4>
<p>For IE 10 (desktop) to work in it's new "snap mode" you need to use this:</p>
<pre rel="CSS"><code class="language-css">@-ms-viewport {
  width: device-width;
}</code></pre>
<p>But that screws up some Windows Phone 8 phones, overriding the meta viewport tag and rendering as far too large on small screens. So the answer, for now, is this gnarly device detection/injection script.</p>
<h4>Longer backstory</h4>
<ul>
<li>Matt Stow: <a href="http://mattstow.com/responsive-design-in-ie10-on-windows-phone-8.html">Responsive Design in IE10 on Windows Phone 8</a></li>
<li>Tim Kadlec: <a href="http://timkadlec.com/2013/01/windows-phone-8-and-device-width/">Windows Phone 8 and Device-Width</a></li>
</ul>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/javascript/fix-ie-10-on-windows-phone-8-viewport/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>

						
		</item>

	
		<item>
			<title>Strip Whitespace From String</title>
			<link>http://css-tricks.com/snippets/javascript/strip-whitespace-from-string/</link>
			<comments>http://css-tricks.com/snippets/javascript/strip-whitespace-from-string/#comments</comments>
			<pubDate>Sat, 05 Jan 2013 14:46:36 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=19750</guid>

			<description><![CDATA[<p>Whitespace, meaning tabs and spaces.</p>
<code class="language-javascript">// Remove leading and trailing whitespace
// Requires jQuery
var str = " a b    c d e f g ";
var newStr = $.trim(str);
// "a b c d e f g"

// Remove leading and trailing whitespace
// JavaScript RegEx
var str = "   a b    c d e f g ";
var newStr = str.replace(/(^\s+&#124;\s+$)/g,'');
// "a b c d e f g"

// Remove all whitespace
// JavaScript RegEx
var str &#8230;</code>]]></description>

			<content:encoded><![CDATA[<p>Whitespace, meaning tabs and spaces.</p>
<pre rel="JavaScript"><code class="language-javascript">// Remove leading and trailing whitespace
// Requires jQuery
var str = " a b    c d e f g ";
var newStr = $.trim(str);
// "a b c d e f g"

// Remove leading and trailing whitespace
// JavaScript RegEx
var str = "   a b    c d e f g ";
var newStr = str.replace(/(^\s+|\s+$)/g,'');
// "a b c d e f g"

// Remove all whitespace
// JavaScript RegEx
var str = " a b    c d e   f g   ";
var newStr = str.replace(/\s+/g, '');
// "abcdefg"</code></pre>
<pre class="codepen" data-height="300" data-type="result" data-href="omyLd" data-user="chriscoyier" data-safe="true"><code></code></pre>
<p>Doesn't work with other types of whitespace though, for instance &amp;#8239; (thin space) or &amp;nbsp; (non-breaking space)</p>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/javascript/strip-whitespace-from-string/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>

						
		</item>

	
		<item>
			<title>Lighten / Darken Color</title>
			<link>http://css-tricks.com/snippets/javascript/lighten-darken-color/</link>
			<comments>http://css-tricks.com/snippets/javascript/lighten-darken-color/#comments</comments>
			<pubDate>Tue, 01 Jan 2013 14:45:54 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=19646</guid>

			<description><![CDATA[<p>The CSS preprocessors Sass and LESS can take any color and darken() or lighten() it by a specific value. But no such ability is built into JavaScript. This function takes colors in hex format (i.e. #F06D06, with or without hash) and lightens or darkens them with a value.</p>
<code class="language-javascript">function LightenDarkenColor(col, amt) {
  
    var usePound = false;
  
    if (col[0] == "#") {
        col = col.slice(1);
        usePound = true;
    }
 
    var num = parseInt(col,16);
 
    var r = (num &#62;&#62; 16) + amt;
 
    &#8230;</code>]]></description>

			<content:encoded><![CDATA[<p>The CSS preprocessors Sass and LESS can take any color and darken() or lighten() it by a specific value. But no such ability is built into JavaScript. This function takes colors in hex format (i.e. #F06D06, with or without hash) and lightens or darkens them with a value.</p>
<pre rel="JavaScript"><code class="language-javascript">function LightenDarkenColor(col, amt) {
  
    var usePound = false;
  
    if (col[0] == "#") {
        col = col.slice(1);
        usePound = true;
    }
 
    var num = parseInt(col,16);
 
    var r = (num &gt;&gt; 16) + amt;
 
    if (r &gt; 255) r = 255;
    else if  (r &lt; 0) r = 0;
 
    var b = ((num &gt;&gt; 8) &amp; 0x00FF) + amt;
 
    if (b &gt; 255) b = 255;
    else if  (b &lt; 0) b = 0;
 
    var g = (num &amp; 0x0000FF) + amt;
 
    if (g &gt; 255) g = 255;
    else if (g &lt; 0) g = 0;
 
    return (usePound?"#":"") + (g | (b &lt;&lt; 8) | (r &lt;&lt; 16)).toString(16);
  
}</code></pre>
<h4>Usage</h4>
<pre rel="JavaScript"><code class="language-javascript">// Lighten
var NewColor = LightenDarkenColor("#F06D06", 20); 

// Darken
var NewColor = LightenDarkenColor("#F06D06", -20); </code></pre>
<h4>Demo</h4>
<pre class="codepen" data-height="300" data-type="result" data-href="EatIr" data-user="chriscoyier" data-safe="true"><code></code></pre>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/javascript/lighten-darken-color/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>

						
		</item>

	
		<item>
			<title>Inject New CSS Rules</title>
			<link>http://css-tricks.com/snippets/javascript/inject-new-css-rules/</link>
			<comments>http://css-tricks.com/snippets/javascript/inject-new-css-rules/#comments</comments>
			<pubDate>Tue, 01 Jan 2013 14:35:29 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=19644</guid>

			<description><![CDATA[<p>If you need to change the style of an element with JavaScript, it's typically better to change a class name and have the CSS already on the page take effect and change the style. However, there are exceptions to every rule. For instance, you might want to programmatically change the a pseudo class (e.g. <code>:hover</code>). You can't do that through JavaScript for the same reason inline <code>style=""</code> attributes can't change pseudo classes.</p>
<p>You'll need to inject a new <code>&#60;style&#62;</code>&#8230;</p>]]></description>

			<content:encoded><![CDATA[<p>If you need to change the style of an element with JavaScript, it's typically better to change a class name and have the CSS already on the page take effect and change the style. However, there are exceptions to every rule. For instance, you might want to programmatically change the a pseudo class (e.g. <code>:hover</code>). You can't do that through JavaScript for the same reason inline <code>style=""</code> attributes can't change pseudo classes.</p>
<p>You'll need to inject a new <code>&lt;style&gt;</code> element onto the page with the correct styles in it. Best to inject it at the bottom of the page so it overrides your CSS above it. Easy with jQuery:</p>
<pre rel="jQuery"><code class="language-javascript">function injectStyles(rule) {
  var div = $("&lt;div /&gt;", {
    html: '&amp;shy;&lt;style&gt;' + rule + '&lt;/style&gt;'
  }).appendTo("body");    
}</code></pre>
<h4>Usage</h4>
<pre rel="JavaScript"><code class="language-javascript">injectStyles('a:hover { color: red; }');</code></pre>
<h4>Demo</h4>
<pre class="codepen" data-height="300" data-type="result" data-href="hinxG" data-user="chriscoyier" data-safe="true"><code></code></pre>
<h4>More Information</h4>
<ul>
<li><a href="http://www.thecssninja.com/javascript/noscope">Style injection quirks in IE</a> (Ryan Seddon).</li>
<li><a href="http://stackoverflow.com/questions/311052/setting-css-pseudo-class-rules-from-javascript/14106897">Stack Overflow thread</a>.</li>
</ul>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/javascript/inject-new-css-rules/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>

						
		</item>

	
</channel>

</rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk (User agent is rejected)
Content Delivery Network via cdn.css-tricks.com

 Served from: css-tricks.com @ 2013-06-18 20:55:24 by W3 Total Cache -->