Writing HTML Dynamic HTML Example - questions External JavaScript |
Start of website Alphabetical index |
The line below is generated by JavaScript. |
<HTML> <HEAD> </HEAD> <BODY> The line below is generated by JavaScript. <SCRIPT TYPE="TEXT/JAVASCRIPT"> var today = new Date () document.write ("<BR>Today's date is ") document.write (today) </SCRIPT> </BODY> </HTML> |
Type something into text box |
<HTML> <HEAD> </HEAD> <BODY> <SPAN ID="big">Type something into text box</SPAN> <FORM ACTION="#" NAME="enlarge"> <INPUT TYPE=TEXT NAME="inp" SIZE=40 ONKEYUP="document.getElementById('big').innerHTML='<H1>'+enlarge.inp.value+'</H1>'"> </FORM> </BODY> </HTML> |
big.innerHTML='<H1>'+enlarge.inp.value+'</H1>'
Unfortunately, while this works in Mozilla as well as Windows, Mozilla gives some warning messages. Since I am trying to give code which follows standards, I will be using the long version. But if you prefer the shorter version (which does work!), then it's up to you.
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> function setup(ans) { lit = '' if (ans == 'anim') { lit = 'How many legs ? ' lit = lit + '<SELECT NAME="q2" ONCHANGE="alert(document.quest.q2.value)">' lit = lit + '<OPTION VALUE="">- Please select -</OPTION>' lit = lit + '<OPTION VALUE="cat">4</OPTION>' lit = lit + '<OPTION VALUE="sparrow">2</OPTION>' lit = lit + '<OPTION VALUE="snake">0</OPTION>' lit = lit + '</SELECT>' } if (ans == 'min') { lit = 'What colour ? ' lit = lit + '<SELECT NAME="q2" ONCHANGE="alert(document.quest.q2.value)">' lit = lit + '<OPTION VALUE="">- Please select -</OPTION>' lit = lit + '<OPTION VALUE="emerald">green</OPTION>' lit = lit + '<OPTION VALUE="ruby">red</OPTION>' lit = lit + '<OPTION VALUE="sapphire">blue</OPTION>' lit = lit + '</SELECT>' } document.getElementById('rep').innerHTML=lit } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME="quest"> <SELECT NAME="q1" ONCHANGE="setup(document.quest.q1.value)"> <OPTION VALUE="">- Please select -</OPTION> <OPTION VALUE="anim">Animal</OPTION> <OPTION VALUE="min">Mineral</OPTION> </SELECT> <SPAN ID="rep"></SPAN> </FORM> </BODY> </HTML> |
I am not going to give a working example of this, as it's a bit complicated. But I want to document it as it's useful.
It is possible to have external JavaScript - that is, a file of JavaScript by itself, which can be loaded into a webpage at run time. Why should you do this? One reason is that you might want a piece of JavaScript used by several webpages. For example, you might want a standard heading to every webpage in your website. You could copy the code into each page, of course, but then, if you wanted to change it, you would have to change every page, which would be a bore. Or you might have a date-dependent routine, which needs to be updated from time to time. If you forgot to change every occurrence of the code, it would matter. Have only one copy of the code in an external JavaScript file, and you avoid this.
First, you need to set up the external JavaScript file. This is just simple JavaScript, without any <SCRIPT TYPE="TEXT/JAVASCRIPT"> or </SCRIPT>. Put the code into functions (you can have more than one). Save it as "name.js" - for example "header.js". It would be sensible to save it in the same folder as the webpages that are going to use it, or nearby. It will have to be on your server, at least.
function pagehead() { document.write('<H2>Header</H2>') }
|
Now you need to write the code inside your webpage to load this code.
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="header.js"></SCRIPT> </HEAD> <BODY> <SCRIPT TYPE="text/javascript"> pagehead() </SCRIPT> The rest of the webpage! </BODY> </HTML> |
So there are two things to do. Reference the external JavaScript file inside HEAD. Use the function you have defined in this external JavaScript file inside a <SCRIPT TYPE="TEXT/JAVASCRIPT"> --- </SCRIPT> in BODY or HEAD. Since you reference the function by name, you can have more than one function in the external JavaScript file, and use each as you want it, where you want it.
© Jo Edkins 2005