Updated the javascript code in the template to make it more stable.