Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c28232d
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: ___ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |