Move JavaScript to External File

  • The next step is to identify the actual JavaScript code that needs to be moved. Generally speaking, you will want to look for a the code. It will start with:

    and end with . Essentially, you’ll want to copy everything (including the script and ending with script) and save it in a text file named something.js (whereas something in the filename describes what that script does. The filename doesn’t really matter, but naming it with a memorable name will help you in the future.






  • Once you copy the JavaScript code out of your html file and saved it into a file called something.js you will need to get rid of the code on your html page and “call the file” instead. So, if you’ve copied the code you can delete it in your html file.






  • Upload or save the something.js text file on your web site somewhere. Some prefer to make a new folder or directory specifically for the external JavaScript files, but they really can be saved anywhere on your site. As long as you remember where they are, you can call them in the html file.





  • In the html code where the JavaScript used to be, you need to call the file. You can do it just like this:
    &ltscript type="text/javascript" src="http://www.domain.com/assets/js/something.js"></script>




  • That is the code for “calling” your JavaScript file named something.js in a folder called javascript.
    After you have saved your html file, go to that web page on your web site. The code will have been moved out of your html page and your html pages won’t suffer from what I call “code bloat” which ultimately hurts your search engine optimization. The more you can focus the content in your html files the better.

    0 comments:

    Post a Comment

    ◄ Newer Post Older Post ►