Skip to content

Instantly share code, notes, and snippets.

@kixxauth
Created November 29, 2012 13:52
Show Gist options
  • Save kixxauth/4169216 to your computer and use it in GitHub Desktop.
Save kixxauth/4169216 to your computer and use it in GitHub Desktop.
Development Mode in Web Browsers
<script>
// Conditionally load Google Analytics
if (!PAGE.inDevmode) {
var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
}
// Conditionally log events
if (PAGE.inDevmode) console.log('page parsed');
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Devmode Test Page</title>
<script type="text/javascript">
var PAGE = (function () {
var PAGE = {
getParam: function getParam(key) {
var params = (window.location.search || '').split('&')
, parts, i = 0
for (; i < params.length; i += 1) {
parts = params[i].split('=');
if (parts[0].indexOf(key) != -1) {
return parts[1];
}
}
return null;
}
, cookieExists: function cookieExists(key) {
var cookies = document.cookie.split(';')
, i = 0;
for (; i < cookies.length; i += 1) {
if (cookies[i].indexOf(key) != -1) {
return true;
}
}
return false;
}
, setCookie: function setCookie(name, val) {
document.cookie = name +'='+ val +'; path=/';
}
, devmode: function (on) {
if (on) {
if (console && console.log) console.log('entering devmode');
PAGE.setCookie('devmode', 1);
return PAGE.inDevmode = true;
} else {
if (console && console.log && PAGE.MODE === 'DEV') {
console.log('leaving devmode');
}
return PAGE.inDevmode = false;
}
}
};
PAGE.devmode(PAGE.cookieExists('devmode') || PAGE.getParam('devmode'));
return PAGE;
}());
</script>
</head>
<body>
<h1>Devmode Test Page</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment