Do you want to add a quick JavaScript function to a standard HTML page? The process is actually fairly simple, and this article explains it.

Steps

  1. 1
    Open Notepad or a comparable text editing application.
  2. 2
    Create the basic HTML skeleton of the website.
    Advertisement
  3. 3
    Add the following JavaScript to the </head> tags.
  4. 4
    Modify the <body> tag, so it calls your function when the page loads:
  5. 5
    Ensure your finished product looks something like this:
  6. 6
    Modify the function name (functionName) and the actual function itself to fit your needs. All done!
  7. Advertisement

Community Q&A

  • Question
    I don't want to use andlt; body onload> as I only need my function when the user clicks on the table cell. What can I do?
    Community Answer
    Community Answer
    Functions don't need to be declared directly inside a tag. You can also put the code: function functionname() { Alert('hello world') } anywhere between the two script tags. As for your question on having the code run after the user clicks the table cell, you can do this. Create the table outside of the script tags normally with HTML, but use the onclick feature in the table cells tag, referencing a javascript function you can create in the script tags.
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 17 people, some anonymous, worked to edit and improve it over time. This article has been viewed 80,647 times.
How helpful is this?
Co-authors: 17
Updated: May 6, 2021
Views: 80,647
Categories: JavaScript
Advertisement