Make javascript snippet better