hjs : Writing hScript in <script> HTML tag

09.08.2008 3269 1

Here you can see how it looks in the HTML document.
index.html :

<html>
<body>
<script type="text/hscript">
	var div = class( "js.Lib" ).document.getElementById( "testDiv" );
	var i = 0;
	div.innerHTML = "test" + i;
</script>
<div id="testDiv"></div>
<script type="text/hscript">
	i++;
	div.innerHTML += "<br/>test" + i;
</script>
<script type="text/javascript" src="hjs.js"></script>
</body>
</html>

You just have to add a js file named hjs.js, that is a kind of bootstrap for hScript.
Once your document is loaded, hjs checks all <script> tags, looks if type is text/hscript, and parse the script.
The hjs has a bootstrap class.
BootStrap.hx :

class BootStrap
{
	var interp	: hscript.Interp;
	
	function new()
	{	
		interp = new hscript.Interp();
		interp.variables.set( "new", createInstance );
		interp.variables.set( "class", getClass );
		
		var s = js.Lib.document.getElementsByTagName( "script" );
		var script = "";
		
		for ( i in 0...s.length )
			if ( s[ i ].getAttribute( "type" ).toLowerCase() == "text/hscript" )
				script += s[ i ].innerHTML;
		parseScript( script );
	}
	
	function parseScript( s : String )
	{
		var parser = new hscript.Parser();
		var program = parser.parseString( s );
		interp.execute( program );
	}
		
	function createInstance( s : String, a : Array<Dynamic> )
	{
		return Type.createInstance( Type.resolveClass( s ), a );
	}
	function getClass( s : String )
	{
		return Type.resolveClass( s );
	}
	
	static function main()
	{
		var v = new BootStrap();
	}
}

In order to get the hjs.js file, we have to build BootStrap like that:
build.hxml :

-js hjs.js
-main BootStrap
-lib hscript

As you can see in the example, all <script> tags are parsed from the top of the document to the bottom.
The variables are shared between different <script> blocks.
Last thing to know is that you have to add the hjs.js file at the bottom of your document in order to be able to access all part of the HTML document.
I can't see if it could be really useful but i found it funny to try ! .

Commentaires

14.08.2008 à 22:15 jdo

the best way to figure it out between the "technical performance" and the real interest in a production mode of this library is to benchmark this kind of sample code, don't you think ?

Laisser un commentaire

http://
×