JS Coding Solutions

When creating and maintaining this website, we occasionally come across coding information that is either confusing or difficult to find from internet sources.
Whilst we do not profess to be coding experts, we have worked out some solutions you may find helpful.
This page includes some of these solutions for 'javascript' codes.

Creating a js File

Javascript files are used to hold text that is common to all or many web-pages, and/or calculation code, both of which can be included in the same file.

The number of javascript files loaded by a page can affect page appearance speed, which can affect its SEO rating. It is recommended that this number is kept to 3 or less (for any given page).

Creating a js File

js files are text files, plain and simple.
You simply create the file in Notepad and save it as a text file (using the UTF-8 formal option) and then change the extension from .txt to .js

The only two things that distinguishes it from other text files are:
Its file extension (.js)
and
Its content is in css code

Including Code Using "document.write"

You create a single js file with one complete set of code to be loaded as many times and in whichever location (on your page) as you wish.
A simple example of text written in a form that can be recovered and used in an html file is provided below:
document.write(
'<div id="header">\
<h1>Hello World</h1>\
<p class="layout">enter your text in here<br/>\
including all of your images<br/>\
and your links<br/>\
and you formatting</p>\
</div>'
)

You then add the following code within the <body></body> of your html page where you want it to appear:
<script type="text/javascript" src="/Resource/javatext.js"></script>

Including Code Using "document.write"

You create a single js file with as many sets of code you wish to be loaded as many times and in whichever locations (on your page) as you wish.
A simple example of text written in a form that can be recovered and used in an html file is provided below:

function firsttext() {
document.getElementById("textone").innerHTML =
'<div id="header">\
<h1>Hello Earth</h1>\
<p class="layout">enter your text in here<br/>\
including all of your images<br/>\
and your links<br/>\
and you formatting</p>\
</div>'

}
function secondtext() {
document.getElementById("texttwo").innerHTML =
'<div id="header">\
<h1>Hello Mars</h1>\
<p class="layout">enter your text in here<br/>\
including all of your images<br/>\
and your links<br/>\
and you formatting</p>\
</div>'

}
document.open();
firsttext();
secondtext();
document.close();

You then add the following code within the <body></body> of your html page where you want the respective texts to appear:
<div id="textone">
followed by:
<div id="texttwo">

Names

Text names in a js file must not duplicate any such names in your .css files.

Our Other Coding Pages:

.html; .css; .htacces; SEO