How to Call a JavaScript File from an Anchor Tag
JavaScript is a client-side scripting language that executes when an enabled browser encounters an HTML <script> tag with the type attribute text/javascript. JavaScript has a host of functions and features that can be used to manipulate Web browsers and browser contents, typically Web page user interfaces. Because Web browsers interpret and execute JavaScript code, it can be embedded in Web pages, run directly from the browser URL bar or requested from other Web pages via hyperlinks by means of the HTML anchor tag (<a>).
Instructions
-
-
1
Open a text editor and create a new file named jsFile.html. Add a JavaScript open script (<script>) tag and a JavaScript close script tag (</script>) to the file.
<script>
</script>
-
2
Assign a type attribute to the <script> tag and give the attribute the value text/javascript. Any code placed between the JavaScript open <script> and close </script> tags will be interpreted as JavaScript and executed in any JavaScript-enabled Web browser.
<script type="text/javascript">
</script>
-
-
3
Use the JavaScript document.write() command to write the text value "This page is called from a hyperlink." to the Web page, and place the command between the <script> and </script> tags. The text will be written to the Web page when it opens in the Web browser. Save and close jsFile.html.
<script type="text/javascript">
document.write("This page is called from a hyperlink.");
</script>
-
4
Use the text editor to create a new file named openJavaScript.html. Add the following HTML tags to the file: <html>, <head>, </head>, <body>, </body>, and </html>.
<html>
<head></head>
<body></body>
</html>
-
5
Add an HTML anchor tag between openJavaScript.html's <body> and </body> tags (<a>) and give the anchor tag the href attribute value jsFile.html. Give the <a> tag the value "Click to open the JavaScript file" and close the anchor tag (</a>).
<html>
<head></head>
<body>
<a href="jsFile.html">Click to open a JavaScript file.</a>
</body>
</html>
-
6
Open openJavaScript.html in a Web browser. Click the hyperlink text "Click to open the JavaScript file" and verify that jsFile.html opens and writes the text "This page is called from a hyperlink" to the Web page.
-
1
Tips & Warnings
To execute JavaScript code in the Web browser's URL bar, precede the code with the text "javascript:"
JavaScript files can be opened in HTML pages by means of the JavaScript window.open() function.
Anchor tags used to open JavaScript files can use any attributes typically used with the <a> tag, such as target, charset, name and tabindex.
Anchor tags used to open JavaScript files can use any event attributes typically used with the <a> tag, such as onblur and onmouseout.
Anchor tags can be used to execute JavaScript functions embedded in the Web page that houses the hyperlink.
Links that call JavaScript files can be styled using CSS.
In XHTML, the name attribute is deprecated and will be replaced with the id attribute.
References
Resources
- Photo Credit Stockbyte/Stockbyte/Getty Images