Created
May 2, 2010 10:07
-
-
Save langalex/387041 to your computer and use it in GitHub Desktop.
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
var styles = heredoc(function() { | |
/* | |
<style> | |
.my_css { | |
... | |
} | |
</style> | |
*/ | |
}); | |
$("head").append(styles); | |
function heredoc(fun) { | |
return (fun + '').replace('function () {', '').replace(/\}$/, '').replace('/*', '').replace('*/', ''); | |
}; |
see http://twitter.com/cramforce/status/13238989508 - firefox excludes comments from the function string. afaik there's no other way, no.
Well, we could have somewhere else docs, i.e. embedded in a web sites dom tree?
<pre id="a12">
The text
</pre>
<script>
var s = $("#a12").html();
</script>
e4x allows to embed XML directly into JS, looks like some kind of heredoc after all:
var languages = <languages type="dynamic">
<lang>JavaScript</lang>
<lang>Python</lang>
</languages>;
(Example from https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X ) . And e4x does have a natural looking syntax for accessing XML nodes.
But I don't know about supporting platforms besides Mozilla.
well my problem is that i have html templates in a javascript file so i can't use the dom trick. the e4x syntax would be perfect but only if it was available in all browsers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know if a function to string conversion is standardized in javascript? Besides, this would not survive javascript compression... is there some other way to embed the heredoc outside of a comment string?