Fixes #1233

This is JavaScript solution for #1233 
Thanks to @Cu8eR & @demonate