improve JS code on change view