Skip to content

Instantly share code, notes, and snippets.

@kristerkari
Last active February 6, 2020 04:00
Show Gist options
  • Save kristerkari/4113133 to your computer and use it in GitHub Desktop.
Save kristerkari/4113133 to your computer and use it in GitHub Desktop.
Lazy evaluating Javascript code
<html>
<body>
<!-- ----------------------------------------------- -->
<!-- inline script block with commented code inside: -->
<!-- ----------------------------------------------- -->
<script id="myjscode">
/*
(function(h,v){function q(b){if(""===m)return b;
b=b.charAt(0).toUpperCase()+b.substr(1);
return m+b}var f=Math,A=v.createElement("div").style,m;a:{for(var...
*/
</script>
</body>
</html>
// JS helper functions:
// -----------------------
function lazyEval(id) {
var lazyElement = document.getElementById(id);
var lazyElementBody = lazyElement.innerHTML;
var jsCode = stripOutCommentBlock(lazyElementBody);
(0,eval)(jsCode);
}
function stripOutCommentBlock(code) {
return code.replace(/^[\s\xA0]+\/\*|\*\/[\s\xA0]+$/g, "")
}
// JS one-liner function:
// -----------------------
function lazyEvalOneLiner(id) {
(0,eval)(document.getElementById(id).innerHTML.replace(/^[\s\xA0]+\/\*|\*\/[\s\xA0]+$/g, ""))
}
// Function call somewhere in the code:
// -------------------------------------
lazyEval('myjscode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment