Skip to content

Instantly share code, notes, and snippets.

@arextar
Created March 25, 2011 02:05
Show Gist options
  • Save arextar/886235 to your computer and use it in GitHub Desktop.
Save arextar/886235 to your computer and use it in GitHub Desktop.
Modules in commented blocks
/*+Module:Alert
alert("this script is loaded but not executed until later called(can be multiple times)")
*/
Module("Alert")
Module("Alert")
/*-Module:RunOnce
alert("this code will only run once")
*/
Module("RunOnce")
Module("RunOnce")
/*+Module:WithData(module)
alert("This module uses data! "+module.data.message)
*/
Module("WithData",{
message:"Hola!"
})
Module("WithData",{
message:"Bonjour!"
})
/*!Module:CommentedOut
alert("this code will not run")
*/
Module("CommentedOut")
(function () {
var executed = {}
this.Module = function (name, data) {
var w = String.fromCharCode(9000),
t = document.documentElement.innerHTML.replace(/\*\//g, w),
a = new RegExp("/\\*\\n?(\\-|\\+)Module:" + name + "\\(?([^\\)]+)?\\)?([^" + w + "]+)" + w).exec(t)
if (a && (a[1] != "-" || !executed[name])) {
arguments.callee[name] = {
name: name,
data: d,
run:a[1]
}
var d = document.createElement("script");
d.innerHTML = d.text = "(function(" + a[2] + "){" + a[3] + "})(Module['" + name + "']);";
document.getElementsByTagName("script")[0].parentNode.appendChild(d);
d.parentNode.removeChild(d);
executed[name] = true
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment