Day 3&4 of the #100daysofMiva Coding Challenge

Hi guys, sorry I’m posting this really late but I have had problems writing this article as I keep seeing some errors while writing and previewing this article which are not from my end and this caused the unusual delay in the posting of this article. …


This content originally appeared on DEV Community and was authored by Ayotomide

Hi guys, sorry I'm posting this really late but I have had problems writing this article as I keep seeing some errors while writing and previewing this article which are not from my end and this caused the unusual delay in the posting of this article. Please bear with me if these errors are still noticeable even after publishing. I refuse to give up😭. Well let's head on to what I learnt during this day 3 and day 4 coding challenge because what I learnt spilled onto day 4.

Linking Javascript to HTML

Linking a JavaScript file to an HTML document is a fundamental skill in web development. It allows you to add interactivity and dynamic behavior to your website. This guide will walk you through the process of linking a JavaScript file to your HTML file.

Why Link JavaScript to HTML?

JavaScript is a programming language that runs in the browser and can manipulate the content, structure, and style of your web pages. By linking a JavaScript file to your HTML, you can separate your content (HTML) from your behavior (JavaScript), making your code cleaner and easier to manage.

Step 1: Create Your HTML File

Start by creating a basic HTML file. If you don’t have one yet, here’s a simple example:

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My First Webpage</title>
</head>
<body>
    <h1>Welcome to My Webpage</h1>
    <p>This is a simple webpage with a linked JavaScript file.</p>
</body>
</html>

Step 2: Create Your JavaScript File

Next, create a new JavaScript file. You can do this using any text editor. Name the file something like script.js. Here’s a simple example of what could be inside:

javascript
// script.js
console.log("Hello, World!");

This JavaScript file will print "Hello, World!" to the browser's console when the page loads.

Step 3: Link the JavaScript File to Your HTML

To link the JavaScript file to your HTML, you’ll use the tag. This tag can be placed in two primary locations within your HTML file: in the <head> or at the end of the <body> section.</p> <p>Linking in the <head>: If you place the <script> tag in the <head>, the script will load before the content is rendered. This can be useful for scripts that need to be loaded immediately, but it can slow down the initial page load if the script is large.<br> Linking before the closing </body> tag: This is the most common practice. By placing the <script> tag just before the closing </body> tag, you ensure that the HTML content loads first. This approach usually results in a faster and smoother user experience.</p> <h2> <a name="step-4-test-your-setup" href="#step-4-test-your-setup" class="anchor"> </a> Step 4: Test Your Setup </h2> <p>To test your setup, open your HTML file in a web browser. Once the page loads, open the browser&#39;s developer console (usually by pressing F12 or Ctrl+Shift+I and selecting the &quot;Console&quot; tab). You should see &quot;Hello, World!&quot; printed in the console. This confirms that your JavaScript file is correctly linked to your HTML file.</p> <h2> <a name="troubleshooting-common-issues" href="#troubleshooting-common-issues" class="anchor"> </a> Troubleshooting Common Issues </h2> <p>Case sensitivity: Remember that file names are case-sensitive on most servers, so script.js and Script.js would be considered different files.</p> <h2> <a name="conclusion" href="#conclusion" class="anchor"> </a> Conclusion </h2> <p>Linking a JavaScript file to an HTML document is simple but essential for building interactive and dynamic websites. By following the steps above, you can keep your HTML and JavaScript code organized, improving both readability and maintainability. Now you’re ready to start adding interactivity to your webpages!</p> <p>Feel free to experiment by adding more functionality to your JavaScript file. Happy coding!</p>


This content originally appeared on DEV Community and was authored by Ayotomide


Print Share Comment Cite Upload Translate Updates
APA

Ayotomide | Sciencx (2024-08-24T17:14:07+00:00) Day 3&4 of the #100daysofMiva Coding Challenge. Retrieved from https://www.scien.cx/2024/08/24/day-34-of-the-100daysofmiva-coding-challenge/

MLA
" » Day 3&4 of the #100daysofMiva Coding Challenge." Ayotomide | Sciencx - Saturday August 24, 2024, https://www.scien.cx/2024/08/24/day-34-of-the-100daysofmiva-coding-challenge/
HARVARD
Ayotomide | Sciencx Saturday August 24, 2024 » Day 3&4 of the #100daysofMiva Coding Challenge., viewed ,<https://www.scien.cx/2024/08/24/day-34-of-the-100daysofmiva-coding-challenge/>
VANCOUVER
Ayotomide | Sciencx - » Day 3&4 of the #100daysofMiva Coding Challenge. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/24/day-34-of-the-100daysofmiva-coding-challenge/
CHICAGO
" » Day 3&4 of the #100daysofMiva Coding Challenge." Ayotomide | Sciencx - Accessed . https://www.scien.cx/2024/08/24/day-34-of-the-100daysofmiva-coding-challenge/
IEEE
" » Day 3&4 of the #100daysofMiva Coding Challenge." Ayotomide | Sciencx [Online]. Available: https://www.scien.cx/2024/08/24/day-34-of-the-100daysofmiva-coding-challenge/. [Accessed: ]
rf:citation
» Day 3&4 of the #100daysofMiva Coding Challenge | Ayotomide | Sciencx | https://www.scien.cx/2024/08/24/day-34-of-the-100daysofmiva-coding-challenge/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.