Hello
 
You can write javascript in a Seprate js file and now whenever you requred you can add the reference of the file and add the javascript in the page