Javascript Link Calling a Function
Displays Random Number in Console

Javascript Link Calling a Function
Displays Random Number in Console

 
This examples shows how to add a custom function. Placing code in custom functions allows you to reuse the same code without repeating it. The function in this case uses the built-in console object’s log method to display a random number in the web browser’s console panel. You need to open the web browser’s web developer tools to show the console panel. This example introduces how to call a function using the a element’s onClick attribute. This attribute’s value is valid Javascript code. Trying to place a lot of complicated Javascript code as the onClick attribute’s value is difficult to edit. The best practice is instead to just add the name of a function followed by the return statement with the false keyword as the expression. Also note that the href attribute’s is set to the hash symbol, # to disable the default browser action for the a element.

Testing to See If It Works

You need a web browser console window open that supports the console object for this example. In that console window when you load or refresh the page you will see the messages showRandomNumber() and a random number such as 0.5579135087318718.

Example of why you see.

If your console window was not enabled when you opened this page, then enable and refresh/reload this page to see again.

Get Javascript Website Skills in 2 Weeks

Javascript website skills needed for interactivity: menus, buttons and forms.