added smart javascript function