Using various JavaScript files within an HTML document

Currently, I am working on coding in P5JS and I have encountered an issue. I am attempting to incorporate multiple JS files into my HTML page, however only the last one is being displayed. How can I make all of them visible? Additionally, I would like to style each one differently. For example, having one at the top of the page, then including other elements such as HTML files or texts, followed by another JS file below them. Is there a way to achieve this using CSS? Please note that I have already linked the CSS file to the HTML page.

I was unable to paste the code directly, so I uploaded it to this image hosting site

Thank you for your assistance.

Answer №1

To include all the js files, you have two options. You can either place them in the head section of your HTML:

<html>
<head>
<script src=script1.js></script>
<script src=script2.js></script>
</head>
<body>
</body>
</html> 

Alternatively, you can also choose to place them at the bottom of the file like this:

<html>
<head>
</head>
<body>
</body>
<script src=script1.js></script>
<script src=script2.js></script>
</html> 

Answer №2

If you want to incorporate multiple p5.js sketches into a single HTML document, one approach is to create p5 instances in order to maintain separate variables and functions within each sketch.

For instance, consider the following code saved as 'sketch.js':

var sketch = function( p ) {

  var x = 100; 
  var y = 100;

  p.setup = function() {
    p.createCanvas(700, 410);
  };

  p.draw = function() {
    p.background(0);
    p.fill(255);
    p.rect(x,y,50,50);
   };
};

var myp5 = new p5(sketch);

You can then import the sketch and embed it within a div element in your HTML like so:

<html>
<head>
    <meta charset="UTF-8">
    <script language="javascript" type="text/javascript" src="libraries/p5.js></script>
    <script language="javascript" type="text/javascript" src="sketch.js"></script>
</style>
</head>
<body>
    <div id="sketch"></div>
</body>
</html>

This process can be repeated for additional sketches, allowing for multiple p5.js sketches in a single JavaScript file.

By organizing your code in this manner, debugging any issues with your sketches and HTML layout should be relatively straightforward.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with Next Js and Typescript, specifically receiving the error TS2304: Unable to locate name 'AppProps'?

I encountered the error message "TS2304: Cannot find name 'AppProps' when trying to launch a basic Next Js project using TypeScript. After searching on Stack Overflow for similar issues, I haven't found any solutions that work for me. imp ...

I am looking to incorporate a rounds div into the backdrop

My attempt to use border-radius did not solve my issue. I am trying to create a circular background for a specific section of the page, as shown in the image. For my first inquiry: I only want the circular background at the top of the page, not the botto ...

Utilizing Angular JS to parse JSON data and showcase it in various tables

Just diving into Angular JS and looking for some guidance. Can someone show me how to parse and showcase JSON Data in separate tables using Angular JS? [ { "id": 0, "isActive": false, "balance": 1025.00, "picture": "htt ...

Removing Items from Orders

Stored in my MongoDB is the following JSON data: [ { "orderItems": [ "606808d2d7351b0c52d38634", "606808d2d7351b0c52d38635" ], "status": "Pending", ...

I am looking to include a popover within my modal using Bootstrap version 3.0.2

Hey there, I'm having an issue where the popover that should be inside the modal is actually appearing outside of it in the top left corner, not visible within the modal itself. Below is my index code: <button type="button" id="example" class="bt ...

Displaying incorrect symbols with icon fonts

I have successfully integrated icon fonts into my simple web page. Check it out on jsFiddle here My design is similar to this example, but instead of bars, I see a snake icon displayed. How can I fix this issue with the fonts? Here is the code snippet: ...

Creating a design layout with Bootstrap 4: Image positioned at the center within a row, accompanied by two text blocks

I would like to center an image within a "row" class and have two text blocks on each side (left and right) of the image. Desired outcome: https://i.sstatic.net/V7KBA.png Current situation: https://i.sstatic.net/9jFsh.jpg Existing code: <sectio ...

Tips for live streaming a video file

I am looking to develop a page in asp.net that plays a video file without allowing it to be downloaded on the client side. It should function like a video on YouTube. ...

Trouble with displaying and hiding elements using multiple Javascript functions

I'm encountering an issue with a JavaScript function - when I implement the first paragraph on its own, it works as expected. However, when I introduce and execute the second paragraph, the show and hide function for "ABOUT THE PROJECT" toggles the te ...

Pass a variable between popup.js and background.js in a Chrome Extension

Despite finding some answers to similar questions, none of them provide a complete solution. The information in the answers lacks necessary documents that were not included in the original question. My goal is to create a scaffold for Chrome extensions wh ...

Could it be that grid-area does not function properly with the attr function, or is this intentional?

The following examples showcase the functionality: It's impressive how I'm utilizing content: attr(class) to streamline the labeling process. Impressive! section { outline: 1px solid red; display: grid; grid-gap: 10px; grid-template-a ...

Can you target an 'input' field that is within a component conditionally rendered using 'v-if'?

Imagine this vue.js template code: <div v-if="y===0"> <input ref="textbox">{{textbox}}</input> </div> In the scenario where y is data retrieved asynchronously, Is there a way to direct focus to the 'input' element onc ...

Achieving the desired alignment of text and button can be easily done using Bootstrap

Is there a way to align text buttons both to the left side and right side using bootstrap? I have included a screen and code below. I apologize if this question seems simple, but I am new to this field. I would like to know how to achieve this, and it woul ...

Discover the secret to extracting a unique style from inspect mode and incorporating it into your CSS design

I'm currently working on my angular project and I've imported the ngx-editor. My goal is to reduce the height of the editor, but I'm facing some challenges. After inspecting the element, I was able to adjust the height successfully within th ...

Cutting imagery to create a new design element

https://i.sstatic.net/IAh0h.jpg There is an image above with 6 gears (circles with pointy edges). My goal is to separate them into individual pictures and, on each picture's hover, have a relevant line with text appear. How can I achieve this? The a ...

Steps for showcasing a automated slideshow

I'm currently working on creating a slideshow that automatically displays multiple images. While the first image shows up just fine, it doesn't seem to transition to the next one. var currentSlide = 0; displaySlides(); functio ...

JavaScript code that loads a specific DIV element only after the entire webpage has finished loading

How can I ensure that the DIV "image" is loaded only after the entire page has finished loading? What JavaScript code should I use? <div class="row"> <div class="image"> CONTENT </div> </div> I plan to execute the functio ...

Using jQuery to replace the dynamic keyword in HTML content

<div class="fotorama__stage__frame magnify-wheel-loaded fotorama_vertical_ratio fotorama__loaded fotorama__loaded--img fotorama__active" aria-hidden="false" data-active="true" style="left: 0px;" href="https://static.domain.com/media/catalog/product/cach ...

Searching for the most recent selected element from a multiple select tag in jQuery

Incorporating a <select multiple="multiple" id="multi"> on my website, I've implemented a jQuery script with the following function: $("#multi").on("change", function(){}) My goal is to retrieve the most recently selected item from the select ...

We regret to inform you that an error has occurred: "Uncaught ReferenceError: data is not defined at (index

I'm currently working in node.js and my main objective is to transfer an object from the server js side to the client js side. I attempted the following: on the server-side route router.get("/", function(req, res){ var data = {name : "gohar" , ...