Menu Javascript Functions
 

JavaScript Functions

The more advanced code that I have been looking at requires a better understanding of JavaScript functions, how they are used to create prototype objects and how these objects are used within the Document Object Model of a webpage. Many of the techniques to enable Accessibilty and more complex page behaviours, such as expandable accordians and other interactive screen devices (modals) are achieved this way.

The Propagation of events and parameter useCapture in the anonymous function.

There are far too many JavaScript functions for me to document and I have no intention of trying! I only look into function as I find them in code that I am using.

The example below calls a JavaScript function when the paragraph with the id="demo" is rendered. It is not easy to see below but the contents of the paragraph is instantiated with the value of 4 times 3.

Function Declarations

W3C say:

" Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked (called upon). It is generally the case that frequently called functions will be placed in a seperate .js file and linked to the pages that call those functions. "

Top

A Script object can also be defined in an HTML file using the <script> .... </script> tags.

The example below calls a function (when the page loads) which performs a calculation and returns the result:

This is the code:

<script>
var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;

function myFunction(a, b) {
return a * b;
}
</script>

The result is:

A JavaScript Function can also be made "inline"

Anonymous Function

An anonymous function is a function without a name. An anonymous function is often not accessible after its initial creation.

Javascript:void(0);

Having been removing the "shim" links from my pages I am now questioning the code that I am in the process of removing. I must have copied the code from somewhere but like a lot of things it does not mean that it was correct in the first place.

There seems to be much debate on whether a Javascript: in an anchor element needs to have the void pseudo included in the syntax. I am careful in my wording of this as I am not sure if I am describing it using the correct terms.

Although this could be academic for this discussion I may want to use the onClick() Javascript call on an anchor element. Actually, the onclick() method would probably apply to other elements. The anchor element already has the event handler for a click and that is normally the destination URL, the onClick() overwrites this handler and is normally used to involke a function when the hyperlink is clicked as opposed to loading the URL. The use of the addEventListener() to an element is a "cleaner" way to do things.

Links

Link to PDF Disabled

If you require a copy please email tempusfugit.me.uk

Top

References: - a note on these

Site design by Tempusfugit Web Design -