Back to Programming main page

JavaScript

Moving elements when the user scrolls
Description Make a layer stay in the visible area of the web page, even when the user scrolls. Sections in the code are included to make the position change when the window is resized. This is useful if you want to have an item always at in the bottom of the screen, for example, like on this website.
Instructions Put InitMenuFollow() into the <body> onload attribute. Create a layer with ID ‘MovingSection’.
Compatability Tested in PC IE 6, Mac IE 5, Firefox and Safari (Mac).
Show/hide code   View example
This site also uses the code, as well as finding the window height. View JavaScript.

Showing/hiding
Description Make a web page element disappear or appear
Instructions Give your element an ID then run the function in an onclick attribute. For example, ShowHide('divLayer1','reverse','reflow')
Compatability Works with all browsers as long as the element you are working on can be shown/hidden by the browser. For Netscape 4 this means using the <layer> tag.
Show/hide code   Example: this very page shows/hides <div> tags containing the code snippets

Showing/hiding depending on window width
Description Make a web page element disappear or appear depending on the width of the user’s browser
Instructions Add the code shown. The parameters for ShowHideWinWidth are the ID of the item to show/hide and the width of the window below which the item is hidden.
Compatability Tested in PC IE 6, Mac IE 5, Firefox and Safari (Mac).
Show/hide code   Example

Find the visible height or document height
Description Find either the height of the visible area or the total height of a web page
Instructions This is not as simple as it should be; see this separate page for full detail.
Compatability Every browser works differently but they should all be able to tell you window or browser height one way or another. The code and example should hopefully work on your browser.
Show/hide code   Example

Create a popup help box
Description Make a <div> block appear on the screen near to where the user clicks, perhaps to provide help
Instructions Create your help box with a <div> tag, ID ‘divHelpTitle’, and another inside called ‘divHelp’. Activate the popup box with a line such as <a href="javascript:void(0);" onclick="HelpPopup('You have clicked on the link to display this text.',event);">Click here</a>
Compatability Works correctly in: IE 4+ (Mac and PC), Firefox, Safari (Mac). For older browsers such as Netscape 4 an alert box appears instead, showing the user the help message.
Show/hide code   Example

Using outerHTML (method 1: custom function)
Description All web browsers allow the text between the tags to be modified via innerHTML. IE sensibly also supports outerHTML for making changes to the tags themselves in addition to the inner HTML. For other browsers, and to follow the W3C standard, there are workarounds.
Instructions Instead of using outerHTML, run the function shown here
Compatability Works with all modern browsers
Show/hide code   Example

Using outerHTML (method 2: prototype)
Description New versions of Netscape (supporting JavaScript 1.5) allow emulation via ‘prototypes’. This method is far less flexible than the previous method.
Instructions Include the prototype setter JavaScript shown here. When code references outerHTML, Netscape will act as though it understands.
Compatability Works in Firefox and other Netscape browsers that support JavaScript 1.5 (not Safari). Does not work in IE but doesn’t need to, though IE5 (Mac) displays an error message.
Show/hide code   Example

Detecting browser details
Description Find the user’s browser name, maker, version, codebase and platform. Note that it is usually better to test if something exists rather than go by browser type as new versions and different browsers may not work in the way you expect. For example, see if the browser supports document.getElementById by doing if(document.getElementById(...)){...}.
Instructions Include all code here. The alert box shows what information can be obtained.
Compatability Works best with Internet Explorer, Firefox, Netscape and Safari. For other browsers it is likely that 'unknown' values will be returned. The most difficult information to obtain is the version number.
Show/hide code   Example