What is JavaScript Void?

104 32

    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="/links/?u=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="/links/?u=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 onclick="window.open('page.html', '', '');return false;">
      New Window
      </a>

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.