Skip to content

Commit

Permalink
first commit of parseScripts
Browse files Browse the repository at this point in the history
  • Loading branch information
padolsey committed May 21, 2009
0 parents commit c28232d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.txt
@@ -0,0 +1,27 @@
parseScripts is copyright (c) James Padolsey
(http://james.padolsey.com)

'parseScripts' is a simple abstraction you can
use the simplify the process of enhancing JavaScript
syntax as you so desire.

To use it, markup your custom content/code in a
SCRIPT element; make sure to give it a custom type
attribute, e.g.

<script type="mycoolstuff">...</script>

Then, at the end of your document (or on doc.ready)
make a call to parseScripts in the following way:

parseScripts('mycoolstuff', function(unparsed){
// Valid JavaScript must be returned
doStuffToParseMyCustomLanguage();

return parsed;

});


--
More info: ___
46 changes: 46 additions & 0 deletions parseScripts.js
@@ -0,0 +1,46 @@
/*
* parseScripts
* (c) James Padolsey
* http://james.padolsey.com
*
* Makes it easier to extend JavaScript as you see fit.
* (uses custom type attributes in SCRIPT elements)
* Please read README.text
*/

function parseScripts(scriptType, parseFn) {

var scripts = document.getElementsByTagName('script'),
sLength = scripts.length,
execute = function(parsed) {
// Execute parsed script in global context.
var dScript = document.createElement('script');
try {
dScript.appendChild( document.createTextNode(parsed) );
document.body.appendChild(dScript);
} catch(e) {
dScript.text = parsed;
document.getElementsByTagName('head')[0].appendChild(dScript);
}
dScript.parentNode.removeChild(dScript);
};

while (sLength--) {
// All script elements matching scriptType are passed to parseFn.
var script = scripts[sLength],
type = script.type,
code = script.innerHTML;
if (scriptType.test ? scriptType.test(type) : type === scriptType) {

if (script.src) {
var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
xhr.open('GET', script.src, false);
xhr.send(null);
code = xhr.responseText;
xhr = null;
}
execute(parseFn ? parseFn(code) : code);
}
}

}

0 comments on commit c28232d

Please sign in to comment.