Archive for the ‘JavaScript’ Category

Drop-in fix for ‘placeholder’ attribute of input tags in Internet Explorer (even IE9)

Sunday, April 1st, 2012

I’ve been using this drop-in replacement for adding the placeholder support for some time now, but now I noticed that it doesn’t work as expected in IE9. What is worse is that it creates problems: while the placeholder functionality works as advertised, it touches inputs that have no placeholder attribute set and creates all sort of havoc. (In my case users could not log in because the script removed the username from forms just because that input did not have it’s placeholder attribute set.)

Nevertheless, here is the revised version that does work as expected even in IE9: (still requires jQuery)

jQuery(function() { = false;
	test = document.createElement('input');
	if('placeholder' in test) = true;
$(function() {
	if(!$.support.placeholder) { 
		var active = document.activeElement;
		$(':text').focus(function () {
			if (($(this).attr('placeholder') != "") 
			&& ($(this).val() == $(this).attr('placeholder'))) {
		}).blur(function () {
			if (($(this).attr('placeholder') != "") 
			&& (typeof ($(this).attr('placeholder')) == 'string') 
			&& ($(this).val() == "" || $(this).val() == $(this).attr('placeholder'))) {
		$('form').submit(function () {
			$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });

Embedding a scripting language in your own C++ programs

Wednesday, October 13th, 2010

One of my current projects has gained the critical mass complexity that can justify an embedding of a scripting language, so that some parts of the projects can be flexibly scripted instead of being hardcoded into the C++ code and thus require a time-consuming compilation every time anything changes.

After reading a little about this, I found out that there are a lot of options, and most of them are really easy!

My first idea was to have two different programs, one in C++ and the other one in PHP (but any other multipurpose script language would suffice), and make them communicate with each other. The best choice for communication would the TCP/IP sockets, and interface could be coded pretty easily. Each function that needed to be exported, could easily be mapped through the network code. Two different programs could even be located on different machines and easily connect through the net.

This solution is possible, and is not that bad actually, but it can get pretty annoying to have two different programs to run and maintain.

So there I came upon the Real solution.

The two script embedding options I liked the most were Lua and JavaScript engine spidermonkey.

Lua is an opensource scripting language, which is very easy to embed and is being used worldwide mostly in games to script the gaming logic. It’s biggest advantage is the speed. By checking different benchmarks, I found out that lua code can be just around 30 times slower than c++ code, which is VERY fast for a scripting language. (For comparison, php is like 300-400 times slower, perl is something like 100-300 times, and Java approx. 10 times).

With this speed, some pretty complex calculations could be easily done in lua, instead of being hardcoded. The other advantage is that lua is semantically extensible, which means that it has very few features out of the box, but you could easily implement almost any paradigm or design pattern, whichever is most suitable for your application.

For example, there are no classes in their usual meaning in lua, they are instead “implemented” in code. When using lua, you are not only writing programs, but also sort of create your own language as you go.

As for embedding, with a small additional opensource library (of mostly macros) Luabind, binding functions and classes of your c++ program to lua becomes a piece of cake. It almost comes down to 1 extra line of code per mapped function, really smooth.

The second option I am considering is JavaScript spidermonkey, which is an embeddable JavaScript engine from Mozilla.

It would be great to have JavaScript in my project, since it’s awesomely flexible, wide-spread and very easy to code in, having a common syntax. Also, for most web developers, JavaScript is a language where you know every class and every function already.

The downside is the speed (approx. 300-400 times slower than native) and the complexity. Also, since the language itself is much more complex than barebone lua, embedding it to applications and mapping functions and classes from c++ can sometimes be tricky and bloated with type conversions and argument checks.

So this seems to be the pro way to do it, so that’s what I am gonna do.  (This time it’s gonna be lua but I am eager to try the spidermonkey some time also).

PS. It’s a pity I have overlooked this at first, but apparently, boost (which you should use most of the time anyway, cz its awesome) has built-in python bindings which work almost as easy (sometimes even easier) than luabind. So that’s a great alternative also, especially if you are a python fan.