moved javascript into if statement