I have some basic html below, and for some reason it's not recognizing the js file I linked in the script tag. Am I linking it incorrectly in some way?: Show
Below is the respective index.js: The ‘src’ attribute in a tag is the path to an external file or resource that you want to link to your HTML document. For example, if you had your own custom JavaScript file named ‘script.js’ and wanted to add its functionality to your HTML page, you would add it like this:
This would point to a file named ‘script.js’ that is in the same directory as the .html file. You can also link to other directories by using ’..’ in the file path.
This jumps up one directory level then into a ‘public’ directory then to a ‘js’ directory and then to the ‘script.js’ file. You can also use the ‘src’ attribute to link to external .js files hosted by a third party. This is used if you don’t want to download a local copy of the file. Just note that if the link changes or network access is down, then the external file you are linking to won’t work. This example links to a jQuery file.
More Information:MDN Article on the HTML Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started Back to Class 9 page » Including JavaScript in your page is a fairly simple process. First, What Is JavaScript?From the W3C Schools Site:
Including the JavaScriptYou can include JavaScript in your HTML in two ways:
For the most part, you will include the JavaScript as an external file. The Script TagThe Here's a very basic snippet of JavaScript using the script tag. This JavaScript is written directly into our HTML page. It will call and alert box as soon as the page loads.
When using the script tag, we must always use the attribute name and value of
Using the script tag to include an external JavaScript fileTo include an external JavaScript file, we can use the script tag with the attribute
This script tag should be included between the JavaScript FilesJavaScript files are not HTML files or CSS files.
It's customary to put all JavaScript files in a folder called js on websites, like so: Simple Demo of Including JavaScriptHere's a very simple demonstration of how to include an external JavaScript file into an HTML page.
Other People's JavaScriptFor this class you are not expected to write any actual JavaScript code. Lucky for you, many of people have already written lots of JavaScript and even allow you to use it for free. JavaScript FrameworksA Framework is basically a library of code for a certain language. Generally, the framework abstracts common tasks and makes it easier and faster for designers and developers to write their specific code. Frameworks don't really do anything by themselves, they just provide an easier platform for people to build on. Common JavaScript Frameworks:
Of these frameworks, JQuery is currently the most popular one. It's also the one you're most like to encounter being used as a buzz word. JavaScript File SizesMany JavaScript files can tend to be rather large, which can slow down the load time of your page. Popular frameworks usually offer a "minified" version of their code. You should always use this version in your pages because it will have a smaller file size. Back to Class 9 page » How do I include a JavaScript file in HTML?To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
Can you include JavaScript in HTML?You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.
How do I link another JavaScript file in HTML?Save the script file with a .js extension, and then refer to it using the src attribute in the <script> tag. Note: The external script file cannot contain the <script> tag. Note: Point to the external script file exactly where you would have written the script.
Can I write JavaScript both within HTML and also as a separate file?JavaScript in <head> or <body>
You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.
|