When you add script to a CEWP, it gets rendered when the page loads. If you then edit the web part, that script still executes, so when you save the changes you've saved the script AND what it rendered. This is one of the reasons why you should store scripts in a central repository and point the CEWP to that specific file (as opposed to putting the raw script inside the CEWP). This model also allows for greater reuse of the script on other pages of the site.
There's a number of formats you can use to link, storing your scripts in a .txt file is perfectly acceptable. Note that you still need the <script type="text/javascript"></script>
blocks around the script. Save the .txt file to a document library, and point the CEWP to the relative path of the document library and text file.
Optional Additional Suggestion: In most environments I work on, publishing is turned on at the rootweb, so I'll typically create a "Scripts" document library on the rootweb and enable versioning on the library. This allows you to leverage the check-in/check-out, publishing, and approval capabilities to better control the management and maintenance of the scripts. Certainly not a requirement, but depending on the scale of what you're doing, this can add some benefits as it locates that content where much of the other "consumed" intranet content likely lives, and gives you sort of a "hub" for maintaining that. While you should never use versioning as a substitute for source control management, it does help you get a better idea of who's making changes and to what, and allows you to "preview" those changes before committing them to everyone.