Skip to content

Instantly share code, notes, and snippets.

@heartcode
Last active December 26, 2015 12:59

Revisions

  1. Robert Pataki revised this gist Apr 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion snippets.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ function getDefaultFontSize() {
    var el = document.body.appendChild(document.createElement('p'));
    el.innerHTML = 'size matters';
    el.style['font-size'] = '1em';
    var fontSize = document.defaultView.getComputedStyle(el)['font-size'];
    var fontSize = document.defaultView.getComputedStyle(el).fontSize;
    document.body.removeChild(el);
    return fontSize.substring(0, fontSize.indexOf('px'));
    };
  2. Robert Pataki revised this gist Apr 26, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion snippets.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ s = s.replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ' ');
    function getDefaultFontSize() {
    var el = document.body.appendChild(document.createElement('p'));
    el.innerHTML = 'size matters';
    el.style.fontSize = '1em';
    el.style['font-size'] = '1em';
    var fontSize = document.defaultView.getComputedStyle(el)['font-size'];
    document.body.removeChild(el);
    return fontSize.substring(0, fontSize.indexOf('px'));
  3. Robert Pataki revised this gist Apr 25, 2014. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion snippets.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,15 @@
    // Replace extra spaces with single ones and get rid of any spaces at the end and beginning of the string.
    // http://stackoverflow.com/questions/6163169/replace-multiple-whitespaces-with-single-whitespace-in-javascript-string - Marku Uttula's answer
    var s = " too much trailing space "
    s = s.replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ' ');
    s = s.replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ' ');

    // Determining the base font size, so that we can use em and rem units in JS
    // TODO - make it foolproof and compatible with browsers, which handle `getComputedStyle()`
    function getDefaultFontSize() {
    var el = document.body.appendChild(document.createElement('p'));
    el.innerHTML = 'size matters';
    el.style.fontSize = '1em';
    var fontSize = document.defaultView.getComputedStyle(el)['font-size'];
    document.body.removeChild(el);
    return fontSize.substring(0, fontSize.indexOf('px'));
    };
  4. heartcode revised this gist Oct 25, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions snippets.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Replace extra spaces with single ones and get rid of any spaces at the end and beginning of the string.
    // http://stackoverflow.com/questions/6163169/replace-multiple-whitespaces-with-single-whitespace-in-javascript-string - Marku Uttula's answer
    var s = " too much trailing space "
    s = s.replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ' ');
  5. heartcode created this gist Oct 25, 2013.
    3 changes: 3 additions & 0 deletions snippets.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    // Replace extra spaces with single ones and get rid of any spaces at the end and beginning of the string.
    var s = " too much trailing space "
    s = s.replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ' ');