It might be better to put this javascript in {% block extra_script %}  (still conditionally).