linking to an external JavaScript file

I'm looking to save an array value in a separate JavaScript file and then use an internal script to create a loop that displays the values of the array stored in the external file. Here's my JavaScript file:

var Arr = new Array("one", "two", "three")

This is my internal script:

<script src="test.js" datatype "text/javascript"></script>
<script type="text/javascript">
for (x in Arr)
{
document.write("<br />"+ Arr[x]);
}
</script>

Answer №1

A more organized approach would be to retrieve those values using AJAX instead of relying on document.write. Additionally, the correct syntax for specifying the datatype should be type="text/javascript" rather than datatype "text/javascript".

Answer №2

Looking at the Arr variable in test.js function

For example, test.js:

(function() {

    var Arr = new Array("one", "two", "three");
});

or

function fn() {

    var Arr = new Array("one", "two", "three");
};

Then in the HTML file:

<script src="test.js" type="text/javascript"></script>
<script type="text/javascript">

    console.log(Arr); // Displays Undefined
</script>

Alternatively, for test.js:

var Arr = new Array("one", "two", "three");

In the HTML file:

<script src="test.js" type="text/javascript"></script>
<script type="text/javascript">

    console.log(Arr); // Outputs ["one", "two", "three"]
</script>

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

Understanding the reactivity and data management in Vue.js through the use of mixins

I am attempting to create a toggle flag that can switch between boolean values of true and false based on specific conditions. // main.js new Vue({ el: `#app`, render: h => h(App,{ props:{ todoID: this.dataset.id } }) } ...

Material-UI - Switching thumb styles in a range slider

Looking for a way to style two entities differently on the Material-UI Slider? Entity A as a white circle and Entity B as a red circle? While using data-index can work, there's a flaw when sliding. Is there a better approach? if (data-index === 0) { ...

Is there a way to automatically log out a user from all open tabs when they log out from just one of them?

I've created a Login page along with several others, and it includes code to verify the session. If a user does not have a session, all pages will reload and redirect to the logout page. This script runs every three seconds. Although the code I' ...

Creating pre-built ES6 npm modules using webpack in a production configuration with uglifyjs

During the production process, I implement a webpack configuration with the UglifyJsPlugin. However, I have encountered an issue related to npm modules that contain es6 syntaxes when deploying to production: An ERROR in the bundle.js file from UglifyJs ...

How can I determine if an npm package requires publishing before actually performing the publish process?

I am working on consolidating multiple projects into a single code base, with each project being publishable. In order to optimize our continuous integration process, I aim for my build agent to execute a single command that publishes all the relevant pro ...

React Bootstrap problem

Having trouble with the grid system - I can't seem to make it work. Here's my code: import React from "react"; import {Col, Row} from "react-bootstrap"; const Home = () => { return ( <Row className=&quo ...

Basic HTML and JavaScript shell game concept

After spending several days working on this project, I am struggling to understand why the winning or losing message is not being displayed. The project involves a simple shell game created using HTML, JavaScript, and CSS. I have tried reworking the JavaSc ...

Disabling form submission when pressing the enter key

Is there a way to prevent a submit action from occurring when the enter key is pressed within an ASP:TextBox element that triggers an asyncpostback upon text change? Instead, I would like it to click on another button. The Javascript function I am using wo ...

Error: Unable to access the 'push' property of an undefined value

42 | <Router> 43 | <ul> > 44 | {this.state.movies.map(movie => <li onClick={() => this.handleMovieClick(movie.title)}>{movie.title}{movie.description} <button type="button" onClick={() => this.deleteMovie(movie.title)}& ...

Connecting icons in Laravel: A step-by-step guide

I have experience linking style CSS using the code: {{ HTML::style('css/styles.css') }} However, I am unsure how to link an icon in Laravel since it is not a standard CSS or JS file. Can anyone provide guidance on this? ...

What is the best way to postpone the loading of an image and show a loading.gif for a few seconds before it appears on the screen? (with the help of jQuery or JavaScript)

My dilemma involves two images: -loading.gif (a loading animation) -scenery.jpeg (the image I want to display after the loading process); Since scenery.jpeg is a small file, using the load() function will simply skip over loading.gif So, when I click t ...

Executing control with AngularJS when ng-change event occurs

When using AngularJS One interesting scenario I encountered is having a ng-change event on a text field and seeing the function being called correctly: <input type="text" ng-model="toggleState" ng-change="ToggleGroupVisiable()" data-rule"" /> The ...

The Increment of DOM Event Detail Stays Unchanged When Angular Click Event is Triggered

Attempting to detect a triple click or touch event in an Angular 7 application. Code snippet from the template: <img id="logoImage" src="/assets/logo.png" (click)="clickLogo($event)"> Code snippet from the component: clickLogo(event) { console ...

Developing a custom JavaScript function to iterate through a group of html elements

In my mission to create a function that loops through a set of HTML elements and gathers their values, I aim to then utilize those values to produce an HTML textarea. Thus far, I've established a series of HTML input boxes and a JavaScript function f ...

Error message occurs when trying to implement Browserify with Gulp: "Navigo is

I'm facing an issue with integrating Navigo (npm package) in my project that uses Browserify and Gulp. This is my file structure (highlighting relevant parts only) -index.html -build -js -modules.js -routing.js -js -modules.j ...

Ensuring the completion of fs.createWriteStream before proceeding with the function is vital to prevent only a portion of the image from being

I am trying to improve the performance of my application by saving images from an API response to a MongoDB database for future use, instead of making repeated requests. Currently, I have implemented a system where I retrieve the image path from the API a ...

Node.js and MongoDB Login Form Integration with Mongoose

I am relatively new to web development and currently working on a simple web page for user login authentication. My goal is to verify user credentials (username & password) on the LoginPage from a mongoose database, and if they are correct, redirect them t ...

Implementing a Push System without using node.JS

I am looking to develop a notification system similar to Facebook's, where notifications appear on the bottom-left side of the screen when someone interacts with your posts, for example. However, my challenge is that I need the server to send real-ti ...

React Component: Issue with toggle functionality in child component not meeting expectations

I'm currently working on a checkbox user interface, where I need to pass a toggle function down to a list component. The issue I'm facing is that while I can check the checkbox successfully, I'm unable to uncheck it for some reason. Below i ...

Creating a user-friendly interface with Dropdown menus paired with Labels and Buttons, structured to repeat row by row

I have a query regarding my design. My concern is about adding products to the bill. Each product needs to be added on a separate line. View Image of Adding One Product per Line Upon adding a product, I also want to display its warranty in the Warranty c ...