Adding variable data from an external file into Google Maps V3

Is it possible to use includes in Java for Google Maps like you can in PHP? Currently, I have code that assigns colors to polygons.

var lineColor = {
"Tornado Warning":                   "#FF0000",
"Severe Thunderstorm Warning":       "#FFFF33",
"Flash Flood Warning":               "#00FF00",
};

As the list grows longer, I want to avoid cluttering the main code. Can I store this color assignment in a separate file and call it instead? Here's what I have tried:

// Assign colors to alerts
    var lineColor = 'xml/alertColors.html';

I created an HTML file with the colors listed as follows:

<html>
<script type="text/javascript">


{
"Tornado Warning":                   "#FF0000",
"Severe Thunderstorm Warning":       "#FFFF33",
"Flash Flood Warning":               "#00FF00",
"Flood Advisory":                    "#00FF7F",
"Special Weather Statement":         "#00FFFF",
};

</script>
</html>

It doesn't seem to be working. Am I doing something wrong or is including files in JavaScript not the same as in PHP?

-Thanks

Answer №1

To implement custom line colors in your project, start by creating a new file called line_color.js. You can choose any name for this file as long as it ends with .js. Then, input the following code:

var lineColor = {
    "Tornado Warning": "#FF0000",
    "Severe Thunderstorm Warning": "#FFFF33",
    "Flash Flood Warning": "#00FF00" // avoid using a comma after the last entry
};

In the <head> section of your main HTML file, include a reference to this JavaScript file like so:

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

You can now utilize these custom settings and variables within your project. For example:

<script>
    alert(lineColor['Tornado Warning']); // This will display #FF0000
</script>

Answer №2

Finally cracked the code. I simply moved the entire var lineColors with its array into a separate .js file, just like how I initially set it up, and then referenced it above the script as before.

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

I didn't need to make any additional calls to the original file since it was already included. This allowed the script to pull the array seamlessly from the external file, resulting in cleaner and more organized code without the lengthy data array.

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

How can I trigger an onclick event for a link automatically upon loading in Laravel?

I am presenting the link below: <a href="javascript:void(0)" onclick="xtenderSearchItem.modal_visibility = ! xtenderSearchItem.modal_visibility;">Request</a> My goal is for it to change the language of the page (which occu ...

The error message thrown states: "Invalid function for Dispatch Action within Redux Toolkit."

Currently, I am in the process of learning the redux toolkit and have encountered a Type Error that I am unable to resolve. Let's Create a Counter Slice! const { createSlice } = require("@reduxjs/toolkit"); const initialState = { count ...

Navigating between pages in ReactJS using Material UI can be made simple and efficient with the

While working on creating a drawer for my dashboard using material-Ui, I encountered an issue with navigating between pages Let me start by sharing my app.js file where the product route is defined: <Provider store={store}> <BrowserRouter> ...

What's the source of this undefined error and is there a way to eliminate it from the code?

After successfully filtering and reducing the data, I encountered an issue with undefined. I am trying to figure out what is causing this and how I can either remove it or make it visible on the screen without being invisible. You can also check the codes ...

Developing a new React application with Access Control List (ACL) and encountering an issue with Casl

I've recently started working with casl and it feels like I might be overlooking something crucial. So, I created a file named can.js which closely resembles the example provided in the documentation: import { createContext } from 'react'; i ...

Guide on implementing react hook form 7 together with Material-UI switch

When utilizing react-hook-form with MUI switch, there is an issue where the initial value does not display on page load despite being set to true. However, upon form submission without any changes, the switches reflect their correct values (true or false). ...

Including HTML elements while cycling through a jQuery collection

Is there a more efficient method of integrating somewhat intricate HTML into a webpage than what I'm currently employing? function display(friends) { $(".row").empty(); $.each(friends, function(index, friend) { var html = '<d ...

Blank advertisements displayed on WordPress due to Google AdSense malfunction

My website on Wordpress can be found at . I encountered issues with the deprecated Adsense plugin for WordPress, so I created my own Adsense account. After verifying my site, I created a new ad unit and inserted its code into the body tag. The code for my ...

Click on a link element within a container and create a pop-up window using jQuery

I'm having trouble creating a popup using jQuery for the second A tag within a div. Here's what I've attempted: $(function() { $(".sites").find("a").eq(1).css("color", "red").dialog({ autoOpen: false, show: { effect: "fade ...

Refreshing search results in Asp.net MVC3 without having to reload the entire page

I am currently working on a website using asp.net MVC3. In one of the views, I display query results managed in the controller using a foreach loop. Now, my goal is to automatically refresh the output of the query periodically without reloading the entire ...

When the cursor hovers, a drop-down menu appears simultaneously

Currently utilizing the Bigcommerce stencil theme, I have encountered an issue where both dropdown menus appear when I hover over a link. My goal is to have only one dropdown menu pop up when hovering over a specific link. https://i.sstatic.net/paVoY.png ...

Implementing a color change for icons in React upon onClick event

export default function Post({post}) { const [like,setLike] = useState(post.like) const [islike,setIslike] = useState(false) const handler=()=>{ setLike(islike? like-1:like+1 ) setIslike(!islike) } return ( <> <div classNam ...

What causes the active slide number to remain the same in Swiper JS pagination when using fractions?

I have been working on setting up an .image-slider__fraction block to display the active slide number out of the total number of slides. However, I am encountering an error in the JavaScript that says "Uncaught ReferenceError: myImageSLider is not defined" ...

Struggling to synchronize the newly updated Products List array in zustand?

Let me clarify the scenario I am dealing with so you can grasp it better. I have a Cart and various Products. When a user adds the product (product_id = 1) twice to the cart with the same options (red, xl), I increase the quantity of that item. However, i ...

Saving the current date in MongoDB using the save method

Is there a way to have MongoDB automatically populate a field with the current UTC DateTime when saving a document? I'm aware of the $currentDate operator, but it seems to only be accessible within the update method. ...

Encountering an error of "undefined" following a successful display of values in the

This is my inaugural angular-node application and I am encountering an issue when attempting to display the value retrieved from a getQuestion method in qa.service.ts. The getQuestion function is responsible for fetching data from the backend based on the ...

What could be causing the HTML5 canvas not to show up on the screen?

<!doctype html> <html> <head> <title>Canvas test</title> </head> <body> <script type="text/javascript"> c = getElementById('canvas'); ctx = c.getContext("2d")' ctx.fillRect(10,10,10,10); </s ...

What is the reason for jQuery selector variables not functioning within namespaces?

I'm struggling to figure out why my code isn't working as intended. I'm trying to declare multiple variables, but something seems to be off. Can anyone help me spot the mistake? var message = (function ($) { var $modalBody = $( ...

Tips for wiping clean and restarting data in a modal?

After closing and reopening this modal, both the old information and new data remain. I aim to reset everything within the modal, wiping out details from both the header and body. Expected scenario - https://i.sstatic.net/Z42Rk.png Actual situation - http ...

Is SWR failing to provide outdated data?

My understanding was that SWR should display the cached data upon page load before refreshing with new information from the API. However, in my Next.js app with a simple API timeout, the "loading" message appears every time due to the 5-second delay I adde ...