Metti il codice js in un file .js poi nel header della pagina html

<head>
<script type='text/javascript' src='ilTuoFile.js'></script>
</head>