Skip to content

Instantly share code, notes, and snippets.

@JonMcL
Last active February 14, 2018 14:14
Show Gist options
  • Save JonMcL/a6c78901194e0f334fb4b078d775885b to your computer and use it in GitHub Desktop.
Save JonMcL/a6c78901194e0f334fb4b078d775885b to your computer and use it in GitHub Desktop.
Adds one or more classes to document root for CSS tests. Classes persist through session.
/**
* Simple utility function to add special classes, prefixed with 'style-test' into
* the document.
*
* Example:
* ?style-test=test1+test2
* Adds classes: style-test-test1 style-test-test2
*
*/
(function(Document, window) {
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var root = document.documentElement, i, style_test, styles;
if (sessionStorage) {
style_test = sessionStorage.getItem('style-test');
}
if (getParameterByName('style-test')) {
style_test = getParameterByName('style-test');
}
if (style_test) {
styles = style_test.split(' ');
for(i = 0; i < styles.length; i++) {
root.classList.add('style-test-' + styles[i]);
}
if (sessionStorage) {
sessionStorage.setItem('style-test', style_test);
}
}
})(Document, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment