Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know this was asked almost a year ago, but I'm putting this answer up as a reference for the next poor sod who has this problem. There are probably better solutions out there, but this one is the only thing I could find/come up with.</p> <p>Since JavaScript doesn't seem to be able to get variables from any <code>.scss</code> file directly, we have to use regex to get the desired value from our file. But since we don't want to make it search though our entire SASS file:</p> <h2>Create a SASS file holding your variables</h2> <p>We create a new file, <code>_variables.scss</code>, for JavaScript to search through. Let's say it looks like this:</p> <pre><code>$menuMobileArrowRotate: 90; $articleScale: 1.1; </code></pre> <h2>Importing it to our main SASS file</h2> <p>Import it to SASS by adding <code>@import "variables";</code> near the top of our stylesheet. We can then use these variables throughout our file instead of manually entering values.</p> <h2>Importing it to JavaScript</h2> <p>I'm going to get JavaScript to read <code>_variables.scss</code> and turn it into a string:</p> <pre><code>var sassVariables = new XMLHttpRequest(); sassVariables.open("GET", 'http://www.use-absolute-url.com/_variables.scss', false); sassVariables.send(null); </code></pre> <p><em>Note: to support IE6 and earlier, replace <code>var xmlhttp = new XMLHttpRequest();</code> with:</em></p> <pre><code> if (window.XMLHttpRequest) { var sassVariables = new XMLHttpRequest(); } else { var sassVariables = new ActiveXObject("Microsoft.XMLHTTP"); } </code></pre> <p><a href="http://msdn.microsoft.com/en-us/library/ms537505%28v=vs.85%29.aspx" rel="nofollow">(Source)</a></p> <p>If I set asynch. to <code>true</code>, Firebug shows me an error claiming that <code>fileContent</code> doesn't exist. And since my file is only 4KB, I'm not going to worry too much about potential performance problems by having to wait for it to load.</p> <p>Now, I'm going to declare my variables:</p> <pre><code>var fileContent = sassVariables.responseText, //a string holding the contents of my file menuMobileArrowRotate = null, articleScale = null; </code></pre> <p>I declared <code>menuMobileArrowRotate</code> and <code>articleScale</code> before I made sure the file actually loaded, since I don't want JavaScript to give me an error and mess everything up just because it didn't load. </p> <p>If everything loads fine, I'm just going to use RegEx to find out the value of each of my variables. At least I'm only going through a tiny file instead of searching through my entire stylesheet.</p> <pre><code>if (sassVariables.status === 200) { //if everything loaded nice and dandy menuMobileArrowRotate = fileContent.match(/(?:menuMobileArrowRotate.\s*?)(\d+)/)[1]; articleScale = data.match(/(?:articleScale.\s*?)((\d+)\.*(\d+)*)/)[1]; } </code></pre> <p>Notice the <code>[1]</code> after <code>.match()</code>: RegEx returns an array, and I only want the first match (which, in my case, is also the only match).</p> <p>For all the JS put together: </p> <pre><code>if (window.XMLHttpRequest) { var sassVariables = new XMLHttpRequest(); } else { var sassVariables = new ActiveXObject("Microsoft.XMLHTTP"); } sassVariables.open("GET", 'http://mysite.com/sass/_variables.scss', false); sassVariables.send(null); var fileContent = sassVariables.responseText, menuMobileArrowRotate = null, articleScale = null; if (sassVariables.status === 200) { menuMobileArrowRotate = fileContent.match(/(?:menuMobileArrowRotate.\s*?)(\d+)/)[1]; articleScale = fileContent.match(/(?:articleScale.\s*?)((\d+)\.*(\d+)*)/)[1]; } console.log(menuMobileArrowRotate); //shows 90 console.log(articleScale); //shows 1.1 </code></pre> <p>I can now use these variables anywhere in my JavaScript file following this bit of code.</p> <h2>Bonus: media query variables</h2> <p>SASS doesn't let you use a variable <em>in</em> a media query. However, I can <a href="http://css-tricks.com/media-queries-sass-3-2-and-codekit/" rel="nofollow">make the whole dang query a mixin</a>, and put it in <code>_variables.scss</code>:</p> <pre><code>@mixin mediaQuery($point) { @if $point == 'mobile' { @media only screen and (max-width: 600px) { @content; } } @if $point == 'somethingElse' { @media only screen and (max-width: 1000px) { @content; } } } </code></pre> <p>In <code>style.scss</code>, I can have:</p> <pre><code>@include mediaQuery(mobile) { //blah } @include mediaQuery(somethingElse) { //blah } </code></pre> <p>I can then use the same process to get the desired values (i.e. 600 and 1000), just with different RegEx.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload