What is JavaScript Void?

by Steven Lacher

JavaScript is a programming language used over the web. Modern web browsers like Internet Explorer, Safari and Firefox all allow you to run JavaScript. JavaScript is a "client side" language. That code is downloaded to the user's computer and run locally when a user visits a web page. JavaScript is a rich language with many features found in other programming languages, including functions. Void is a JavaScript function.

The SCRIPT tag

JavaScript code can be written directly into the HTML source of a page, or placed in a separate file. You will need to use the SCRIPT tag, regardless of where you place the Javascript code. Some HTML tags like SCRIPT can be modified with parameters called attributes. If you're writing your JavaScript code in the HTML document, use the language attribute and set it to Javascript. Write your JavaScript code after this opening tag. If you're referencing JavaScript code stored in a separate file, use the type and src attributes for the opening tag. Set the type attribute to "text/javascript" and the src attribute to the name of your file. In either case, remember to use the closing </script> tag.

Functions

A function is a discrete block of code that can be called repeatedly. Functions don't run when the page loads in the browser, but when they are called by code, or some action of the user. Functions can be written with input variables, which pass values to the function. All functions return a default value of "null." The "return" keyword passes a specified value back to the statement that called it. While users can write their own functions, JavaScript also contains a number of built-in functions, like Void.

Void Function

One technique for calling functions is to embed them inside an anchor link: <a href="javascript: alert('Here's your alert!')">Show Me An Alert!</a>. A browser will try to open any clicked href link and load the page it represents. The alert function allows us to remain on the same page and not load a new one because the alert function returns a null value. The browser sees this null and stops, knowing that there is nothing to be loaded. The Void function's purpose is to return null, without doing anything else, thus cancelling the page load. Void must be called with a parameter.

Example

You can embed the void function inside a hyperlink that opens another browser window. Look at this example code: <a href="javascript:void(window.open('page.html', '', ''))">New Window</a> When the user clicks the "New Window" link, JavaScript will open the page.html file in a new browser window, while leaving the current window on its original page. The void function cancels the page load, while allowing the window.open code to be executed.

Alternative to Void

Void can sometimes cause errors. One alternative is to use an href that points to "#", a non-existent url, and call your code in the 'onclick' event of that hyperlink. This can be problematic, though because many browsers will search the page for the "#" anchor link, and reload, or jump to the top of the site page. You can prevent the browser from evaluating the hyperlink altogether, by ending your onclick code with the statement 'return false'. The previous example, using this technique, would look like this: <a href="#" onclick="window.open('page.html', '', '');return false;"> New Window </a>

About the Author

Steve Lacher has been a skilled technical consultant since 1994. He has written training materials and articles for technical journals such as "Domino Power Magazine," taught on television, been a developer and performed many other tasks related to the use of technology in business. Lacher holds a Bachelor of Arts in writing seminars from the Johns Hopkins University.

Photo Credits

  • photo_camera Keyboard image by S.Q T from Fotolia.com