To include in-line Javascript in an HTML document, use the syntax:
<script type="text/javascript">
<!--
//Script code goes here
//-->
</script>If your code defines new Javascript functions, put the <script> tag in the <head> section of your HTML document to make sure the functions are available to all parts of the document. If you want your Javascript to execute inline, for example, to print a customised part of the page, the code may not need a function declaration and the tag should be in the <body> at the point where the output is required.
The comment markers <!-- and --> are required because older browsers don't recognise the <script> tag and would then display your code as text in your page. Always put a Javascript comment marker // in front of the closing --> HTML comment marker or you will get a Javascript error.
The language attribute is not part of HTML4. It was used to stop the code being executed by certain browser versions by specifying a later version number than supported by the browser. Instead, use the Javascript code itself to check the version of the browser and level of Javascript support.
To import external Javascript use this syntax in the HTML:
<script src="URL" type="text/javascript">
The URL specified must be a plain text file with the .js file extension that contains only the Javascript code itself, not the <script>, </script> tags. Comment markers are optional.
For information on the Javascript language itself, see the
Javascript Pocket Reference and search your browser cache for .js files - you can learn a lot from working code from other sites.
Top
var length = document.demo.inputselect.length;document.formname.selectname.options[];options[] array is most useful when you also have access to the selected index (counting from zero):var document.demo.inputselect.selectedIndex;
function mychange() {
var index = document.demo.inputselect.selectedIndex;
var text = document.demo.inputselect.options[index].text;
var value = document.demo.inputselect.options[index].value;
alert("CodeHelp: glossary: Select.onchange(): index = "
+ index + ", text = " + text + " and value = " + value);
}
<FORM METHOD=POST ACTION="/cgi-bin/dummy.pl">
<SELECT ID="inputselect" size="3" onchange="mychange();">
<option value="chosen1">option1</option>
<option value="chosen2">option2</option>
<option value="chosen3">option3</option>
<option value="chosen4">option4</option>
</SELECT></FORM>Documents that use SSI should be identified to the server by using the
.shtml extension.
Top
Along with <div>, span was especially created as an HTML tag for use with style sheets. The div tag divides the content on screen using a line break above the <div> and below the </div> tag. <span> </span> tags are displayed without line breaks.
Top
The src attribute contains the URL of the resource to be used. For details of how src is used with specific tags, see the glossary topics on the tag name:
Not all ISP's and webhosts support SSI, particularly with free webspace, because of the security implications. If your webspace has access to a CGI portal or if you have your own cgi-bin available, you might have SSI available too. Check with your ISP or webhost.
SSI is incorporated into your pages using comment markers, so are ignored by your local browser. Use SSI to place server information like the date of the last modification to the HTML document, execute certain inline CGI scripts like text counters, or include commonly repeating sections of HTML. Remember to use the .shtml extension on these files.
<!--#SSIcommand args="values"-->
Top
var string = new String("content");length - 1.var length = string.length;<a name="link">content</a>
Use:var anchor = string.anchor("link");<a href="file.html">content</a>:var link = string.link("file.html");<big>:string.big();<b>:string.bold();<i>:string.italics();<small>:string.small();<sub>:string.sub();<sup>:string.sup();var lower = string.toLowerCase();var upper = string.toUpperCase();var char = string.charAt(3);var string2 = new String("more content");
var spacer = new String(" ");
var newstr = string.concat(spacer,string2);var index = string2.indexOf(spacer,0);length-1,
(return value is the position of the first match, counting from zero):var index = newstr.lastIndexOf(spacer,(newstr.length-1));
Return a substring from a string (original string remains unchanged):var section = newstr.substring(0,2);Cascading style sheets: Users of some browsers can set up their own stylesheets that will be applied to all sites the user visits. Eventually, one of these settings will conflict with another setting on the web site. The style sheets are applied in a cascade to avoid this problem. The site style sheet will overrule the visitors style sheet. Also, if two site rules conflict, the more specific rule will be applied, ID being more specific than class and class being more specific than HTML tag alterations. If all else fails, the last rule applied takes precedence, so if you add a style tag to one document that conflicts with the external stylesheet, the style tag is applied.
Top
When you only want to apply a set of style rules to one document, or when you want to customise the usual rules for the site, add a <style> tag in the <head> section of the HTML document. As with the <script> tag, it is best to hide the rules from older browsers using comment markers and a mime-type needs to be specified. The syntax is:
<style type="text/css">
<!--
-->
</style>
Top
When you only want to apply a set of style rules to one element or tag, or when you want to customise the usual rules for the element or tag, add a style attribute.
<p style ="color:blue;text-align:center;">
This results in
text in blue and centre aligned
document.formname.submitname.value = "new text";<input type="reset" name="ID" onclick="my_clickfunc();">