Last active
February 6, 2020 04:00
-
-
Save kristerkari/4113133 to your computer and use it in GitHub Desktop.
Lazy evaluating Javascript code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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