|
Javascript CodingHere are my notes on coding Javascript files for dynamic web pages. | Topics this page:
|
Related: |
Best resources to learn JavaScript
To avoid intricate coding necessary to ensure JavaScript coding works across various browsers, most JavaScript coders today use a JavaScript library.
jQuery
MicroJS.com from Thomas Fuchs lists micro libraries of JavaScript.
End of life was announced in 2014, but Yahoo's User Interface IYUI) Javascript library, supported with blogs by developers and users, offers drag-and-drop libraries and controls for AutoComplete Calendar Container (including Module, Overlay, Panel, Tooltip, Dialog, SimpleDialog) Logger Menu Slider TabView new! TreeView
Javascript Examples Used In This Site
Javascript examples used in this site:Stupid Javascript Tricks used by this site: |
|
Timeline For Markup Languages
W3C's SMIL (Synchronized Multimedia Integration Language) standard (and *.smil file extension) integrates digital snapshots with audio commentary. If your browser doesn't support it, get the MathML (Math Markup Language) was defined to describe mathematical symbols. The main competititor to Javascript is Macromedia Flex. |
Only Netscape recognizes <SCRIPT language= "Javascript 1.1">. IE will ignore it. Likewise, Netscape ignores language="VBScript" container tags.
|
|
DHTML
Internet Browsers such as Microsoft's Internet Explorer (IE) and Netscape's Navigator format and present the text and graphic files written in HTML (HyperText Markup Language) Version 3.2 and Version 4 defined in 1997 and achieved Recommendation status 24 December 1999 XML (Extensible Markup Language) — a tranformation language which allows users to change the meaning of HTML tags. CSS (Cascading Style Sheets) (now at Level 2) which allows for precise control over the presentation of Web pages, the DOM (Document Object Model) standard set of function calls for manipulating the content of XML (and HTML) files using ... Scripting languages used to define the events and methods which manipulate objects and their properties such as mouseover() and mouseout() functions. However, XHTML1 extends HTML 4.0 and XML 1.0 to provide Extended Forms handling. W3C says IE 5 “includes a number of experimental features which uses a CSS-like syntax but which are not part of any W3C specification.” Microsoft has since submitted their DHTML spec to W3C for consideration as a standard. Microsoft defines the term dynamic as the ability to alter content at runtime . Netscape's definition of dynamic is limited to user-defined changes only at load time and switching layers at runtime. Microsoft's propriety enhancements to HTML,are implemented through a combination of multimedia objects and ActiveX Controls, include: The hierarichical nature of the Object Model provides for event bubbling where the definition of action events "bubble" up all the way to a global style sheet for a site which defines default handling. |
Using Dynamic HTML to create an animated navigation bar by Shelley Powers. Microsoft's DHTML & CSS MSDN Web Workshop Table of Contents The HTML Tidy utility cleans up markup errors, removes unnecessary items, and restructures the markup to make it easier to read and maintain. DynamicDrive.com specializes in DHTML scripts. |
|
Basic HTML Coding
<TITLE> ... </TITLE> <script type="text/javascript" src="/a4j-simpleRepeator/org.ajax4jsf.resource/org.ajax4jsf.framework.ajax.AjaxScript"> <STYLE ... </STYLE> <BODY>
Input text: <input type="text" name="_id0:_id3" value="" onkeyup="A4J.AJAX.Submit('_viewRoot','/a4j-simpleRepeator/demo.jsf',this,{'parameters':{'_id0:_id4':'_id0:_id4'}})" size="50"> Response: <span id="_id0:repeater"> <input type="hidden" name="_id0" value="_id0"> </form>
Favicon (Favorite Icon)The .ico file in the HEAD section associates the favicon (pronounced fav-eye-con) with the page. The favicon.ico file contains image files (See favicon.com)This specific link is required when HTTPS is used, or the default favicon will not be displayed.
|
|
The Basic Elements of Scripting Style
|
Code Conventions for Javascript Making Your Web Content Accessible, with templates, by Scott Nesbitt Useractive.com HTML Tutorials. HTML Sourcebook by Ian Graham in Canada. The HTML Primer makes NCSA one of the most popular sites for learning and exploring HTML. The Introduction to HTML by Ian Graham at the University of Toronto was recognized by Byte and other magazines as the best available book on HTML and Web technologies. HTML Quick Reference by Michael Grobe at U. of Kansas Microsoft's HTML for Beginners and Workshop on DTHML, HTML, CSS. Microsoft's Typography on the Net. Download from Microsoft Web Fonts Verdanda, Comic Sans MS, Trebuchet MS, Arial Black, Impact, Webdings, Montotype, Gerogia See how different colors look together. Visibone's color chart is slick with its swatches. Also consider Earthlink's Text Font Color pallet and background Colors by Name from Adrian Bolch, Baylor, and Hidaho. Note Failure of AliceBlue color in Netscape 2 and 3 (and indentation error too) Matthew Clemente's FlamingoLingo's HTML 4 weekly e-letter discusses HTML 4, DHTML, CSS, JavaScript, and other MSIE tricks. Input data in your web pages with the Frames Tutor and Tables Tutorials from Dave Barta and Jay Burns. NetbieNET also has a great tutorial on HTML Frames.
|
|
Variables in Javascript
|
|
|
Window
document.title The Window status bar at the bottom
self.status
A clock is there because of RecursiveClock() running in the <HEAD> section.
To break out of frames: if (parent.frames.length > 0){ parent.location.href = location.href; }To open a new window: function popUp(URL,title) { eval(title+" = window.open(URL,'" +title+ "',toolbar=0,scrollbars=0,location=0, statusbar=0,menubar=0,resizable=0, width=500,height=500, left = 250,top = 35"); "); }Method window.close();" is called to close a window: For a page to close itself automatically, in its body tag: <body onLoad="setTimeout('window.close()', 10000);">
10000 means 10 seconds.
|
Javascript Coding TutorialsJavaScript for the Total Non-Programmer is a great first site to visit.W3School's Javascript Examples Selena Sol's monthly column for the cultural anthropologist and other liberal arts hackers gone Webmaster has excellent tutorials. Senior Product Manager for Netscape Communicator, Eric Krock's Introduction to Cross-Browser, Cross-Platform, Backwardly Compatible JavaScript and Dynamic HTML
|
|
Cascading Style Sheets
<link rel="stylesheet" href="nn4.css" type="text/css" media="screen" />
For newer browsers, specify style sheet in an URL @import statement.
<!--link before import prevents Flash Of Unstyled Content in ie6pc -->
<style type="text/css" media="screen">@import url("real.css"); </style> Styles in this file override the nn4.css styles. Style specifications are enclosed between <style> tags. This example defines the default font for the page:
<style type="text/css">
body { font-family: Verdana; Tahoma, Ariel, "Times New Roman", non-serif; } </style> This table describes stylesheet rules, which are composed of selectors and declarations that define how styles will be applied. The selector (a redefined HTML element, class name, or ID name) is the link between the HTML document and the style. There are two different kinds of selectors: types (HTML element tags) and attributes (such as class and ID names). A CSS declaration has two parts: a property ("color") and a value ("red"). The basic syntax of a rule selector {property 1: value 1; property 2: value: 2} " An example (containing two declarations) P {font-size: 8pt; color: red}
|
HTML Tidy cleans up Web pages. Adding a touch of style", Dave Raggett's introduction to CSS. OverLib offers Javascript which create baloon help as you move your cursor over an object. CSS Tutorial and Core Style Sheets Douglas Bowman's "Sliding Doors of CSS" article is a masterwork on creating 3D tabs using CSS. Tags & Styles “Your complete source for CSS & HTML 4 reference” Personalizing a site by switching style sheets improved for NN4 Glish's list of CSS resources and tutorials include a Blogger template in CSS that is XHTML 1.0 compliant, free of tables, relying exclusively on CSS for formatting. Pre-5 browsers will simply see an un-styled but well structured HTML document. 5+ browsers will see a styled page with a 3 panel layout.
|
Generate HTMLThe following Javascript generates all the body HTML of a page using W3C's DOM Level 1 - interface. Counter var j is used to define rows. Counter var i is used to define columns. <HTML><HEAD><script> function start() { var mybody=document.getElementsByTagName("body").item(0); mytable = document.createElement("TABLE"); mytablebody = document.createElement("TBODY"); for(j=0;j<2;j++) { mycurrent_row=document.createElement("TR"); for(i=0;i<2;i++) { mycurrent_cell=document.createElement("TD"); currenttext=document.createTextNode("cell is:"+i+j); mycurrent_cell.appendChild(currenttext); mycurrent_row.appendChild(mycurrent_cell); mytablebody.appendChild(mycurrent_row); } mytable.appendChild(mytablebody); mybody.appendChild(mytable); mytable.setAttribute("border","2"); } } </script> </head> <body onload="start()"> </body> </html> NOTE: The code above uses native JavaScript. But most front-end UI developers now use jQuery instead. Veena Basavarajpresents at LinkedIn blogged and spoke about using the Dust.js library after evaluating other isomorphic JavaScript templating engines. Dust bills itself as a "Less Logic". Dust can run on both browser client machines AND on servers (on Rhino with Java via JNI and V8 with C++ programs). JSON data is most commonly retrieved from a node.js using Express to respond to API requests from the browser. Dust is used to move away from JSP with XML. Dust can be compiled for better performance. Lynda.com has a course on JavaScript Templating. Dust uses curly braces to insert sections within HTML markup. If/then logic. Helpers include - |
JavaScript ReflectionThe method document.getElementsByTagName("body") returns a list of all the elements in the document that have tag named "body", which is available by default (does not need to be created like the other elements. If you view the source from the internet browser, you will only see the code above, unless you install add-ins:
Free HTML Source Bar IE Plug-in HTML displays the final rendered HTML source for pages viewed in IE.
To change the properties associated with all instances of an element, use the item(n) method for an array of items (starting from 0). function changeElementsStyle(el,fw,fs,clr){ if(document.getElementsByTagName)//check for obj { var nodes = document.getElementsByTagName(el) for(var i=0; i<nodes.length; i++){ var nodeObj = nodes.item(i); nodeObj.style.fontWeight = fw; // 'bolder' nodeObj.style.fontStyle = fs; // 'normal' nodeObj.style.color = clr; // 'red' nodeObj.style.background="rgb(255,0,0)"; } } To hide a cell, specify the display property:
nodeObj.style.display="none";
|
HTML Element TagsHTML element tags define how text is presented. For example, <H1> around an element of text defines the size and boldness of that text. "H1" is called a style selector .ClassesClasses are used to create grouping schemes among styled HTML tags by adding the style definition of a particular class to the style definitions of several different tags. In the stylesheet, a class name is preceded by a period (.) to identify it as such:.foo {property 1: value 1; property 2: value 2}A very simple example:
The tags and classes can then be used in combination:
Or not: <p>This is rendered as 10-point sans-serif text in the default color.</p> The ID attribute is used for a uniquely defined style within a stylesheet. In the stylesheet, an ID name is preceded by a hash mark (#) to identify it as such:
Text-Level Attributes: <SPAN> and <DIV> The <span> tag is generally used to apply a style to inline text: <p><span class="foo">This text is rendered as foo-style</span> and this is not. The <div> tag is generally used to apply a style to a block of text, which can also include other HTML elements:
The style attribute provides a way to define a style for a single instance of an element: <p style="font-size: 10pt; color: red">This text is rendered as red, 10-point type</p> The class, ID, and style attributed can be applied within the <span> and <div> elements. Used with class or ID, the <span> and <div> tags work like customized HTML tags, letting you define logical containers and apply a style to their contents. This code defines the POSITIONing of a container for text to be displayed:
<DIV ID = mytext STYLE = "POSITION: relative; WIDTH: 40%; FONT-FAMILY: garamond, serif; FONT-SIZE: 16pt; COLOR: blue; "> <P> Text to be displayed. </P> </DIV> </BODY> DynamicDrive.com offers this tip for defining a watermark image:
<script language="JavaScript1.2">
if (document.all) document.body.style.cssText="background:white url(notebook.jpg) no-repeat fixed center center" </script>
|
W3C's Cascading Style Sheets, level 2 CSS2 Specification W3C's Errata in REC-CSS2-19980512 CSS-Ask Dr Web About Cascading Style Sheets" from Zeldman.com Style Sheets Guide -- from Webmonkey Authoring Style Sheets links -- from Webmonkey “Why Windows Web Pages Have Tiny Text“ By Geoff Duncan for TidBITS explains the history of why fonts on web pages appear smaller on Macs than Windows screens. Tests of JavaScript/DHTML for Gecko&IE5[ W3C DOM - based new DHTML ] & cross-DOMs by Toshirou Takahashi
|
No Right Clicking
<SCRIPT LANGUAGE="JavaScript1.1">
function right(e) { if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2))
return false; return true; } document.onmousedown=right; document.onmouseup=right; if (document.layers) window.captureEvents(Event.MOUSEDOWN); if (document.layers) window.captureEvents(Event.MOUSEUP); window.onmousedown=right; window.onmouseup=right; // End --> </script>
|
Javascript Object Model
The Tito Javascript Debugger and Profiler for IE is advanced. The Whitefrost JavaScript Profiler by Stephen W. Cote, who wrote the engine application framework for client-side development and Web applications Javascript Profiler context-menu extension for Internet Explorer |
Microsoft Document Object Model
|
|
Widow.Open Options
|
Elements in the DOM
textrange refers to the entire document .
element.outerHTML
refers to the entire element -- all text and HTML tags of the element.
element.innerHTML
refers to the text and HTML tags nested between HTML paragragh <P>
tags. (such as <I> or<B>).
element.outerText
refers to all of the text stream and inner HTML tags.
element.innerText
refers to the text stream between tags nested within other tags.
Changes to this value would leave formatting unchanged.
|
|
Client-Side Data BindingThis example is implemented in the Sortable HTML Entity Codes web page. The GUID for the CLASSID in this example points to the Microsoft Tabular Data Control (TDC) that shipped with all versions of IE4. It retrieves (for read-only access) the entire text file to the client IE4 browser. This ActiveX Data Source Object (DSO) — a part of Microsoft's OLE-DB API — uses the HTTP protocol and COM interface mechanisms to enable sorting and filtering on the client machine. <BODY ... <OBJECT id=companies classid="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83" border="0" width=0 height=0> <PARAM name="DataURL" value="entcode.txt"> <PARAM name="UseHeader" value=True> </OBJECT> <DIV DATAsrc= "#companies"DATAFLD="Name"></DIV>
When the UseHeader attribute value is True, DATAFLD refers to the field names specified in the first line of the text file rather than default field names "Column1", "Column2", etc. For example, user header name "Price:INT" specifies the price field as an integer. "picture:HTML" specifies the picture field as containing HTML code which needs to be interpreted.
Name,Price:Float,Items:INT,LastInventoryDate:Date,Image:HTML
| With TDC, only data is downloaded for the browser to format. This is instead of using document.write() Javascript code and wrapping HTML page formatting code around data. The <OBJECT tag defines the data source on the web server which the <DATAsrc="data source" and <DATAFLD binds to <SPAN or <DIV data consumers presented within <TABLE tags. (<SPAN does not work with single record retrievals.) This approach is faster for users, gives them better control, and allows for more server scalability. RDC to access SQL databases is needed to write data back to the server. To encode a string to HTML entity codes: function htmlEncode(s) { return s.replace( /[<>&]/g, function(m) { return "&" + m.charCodeAt(0) + ";"; }); }
|
Cookie Programming
Once a cookie has been sent, the web browser returns the cookie information back to the webserver on each subsequent request to the server until the cookie expires or is deleted. |
Browsers Optimizing Engines
CKEditor is a WYSIWYG HTML text editor for web content creation by bringing common word processor features to web pages. It's free, open source for community maintenance. $49 CK Packager compresses Javascript. Compress HTML removes unnecessary white space and characters such as carriage returns, line feeds, spaces and tabs. It can also remove certain HTML tags. HTMLProtector offers 2 JavaScript based encryption methods to protect web page content by preventing visitors from printing the page and viewing your source code, It also stops spam robots from extracting email addresses from your pages and automated downloads of your entire web site to their hard drive. |
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |