/    Sign up×
Community /Pin to ProfileBookmark

Make JavaScript variable external data?

In a script tag in my HTML file I have a JavaScript function as such. Notice the part where it I create the variable const event = { . My question how in the same folder as the HTML file could I create a file that is only the event variable and refer to in in the code. Could this externally referenced data be a JSON file? Thanks

```
async function insertEvents() {
let response;
try {
const event = {
'summary': 'Martha Dentist',
'location': '800 Howard St., San Francisco, CA 94103',
'description': 'Take Martha to dentist.',
'start': {
'dateTime': '2023-02-28T07:00:00-07:00',
'timeZone': 'America/Los_Angeles'
},
'end': {
'dateTime': '2023-02-28T08:00:00-07:00',
'timeZone': 'America/Los_Angeles'
},
'recurrence': [
'RRULE:FREQ=DAILY;COUNT=8'
],
'attendees': [
{'email': '[email protected]'},
{'email': '[email protected]'}
],
'reminders': {
'useDefault': false,
'overrides': [
{'method': 'email', 'minutes': 24 * 60},
{'method': 'popup', 'minutes': 10}
]
}
};
const request = {
'calendarId': 'primary',
'resource': event
};
response = await gapi.client.calendar.events.insert(request);
} catch (err) {
document.getElementById('content').innerText = err.message;
return;
}

const events = response.result.items;
if (!events || events.length == 0) {
document.getElementById('content').innerText = 'No events found.';
return;
}

}


```

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumFeb 14.2023 — @phagen#1650649

Single backticks (probably created by the button `&lt;/&gt;</C>) won't work reliably when posting code. You better use code tags: <C>your code here</C> or triple backticks. I edited your posting accordingly.

Yes, you can easily place the event's data into an external file, and with JSON you are on the right track. Put the definition only:
<CODE>
`<i>
</i>{
"summary": "Martha Dentist",
"location": "800 Howard St., San Francisco, CA 94103",
"description": "Take Martha to dentist.",
"start": {
"dateTime": "2023-02-28T07: 00: 00-07: 00",
"timeZone": "America/Los_Angeles"
},
"end": {
"dateTime": "2023-02-28T08: 00: 00-07: 00",
"timeZone": "America/Los_Angeles"
},
"recurrence": [
"RRULE:FREQ=DAILY;COUNT=8"
],
"attendees": [
{
"email": "[email protected]"
},
{
"email": "[email protected]"
}
],
"reminders": {
"useDefault": false,
"overrides": [
{
"method": "email",
"minutes": 1440
},
{
"method": "popup",
"minutes": 10
}
]
}
}<i>
</i>
`</CODE>
into that file, named e. g. event.json. I encountered two errors when using you original definition:
<LIST type="decimal"><LI>1. Double quotes for the keys are mandatory in JSON.</LI>
<LI>2. Calculation was marked as an error, obviously fixed values are permitted only.</LI></LIST>

Then load it by this code:
<CODE>
`<i>
</i> const event = await fetch('event.json').then(response =&gt; response.json());
// now you can use the variable event just as in your initial code<i>
</i>
``
Copy linkTweet thisAlerts:
@phagenauthorFeb 14.2023 — First super thanks for fixing the code layout. Second OK, I see what you mean I think the first times I tried it I had know idea about "await". To learn more I think that is a requirement of an asyn fuction, correct? However this is great, exactly what I wanted to do. Thanks so much!
Copy linkTweet thisAlerts:
@SempervivumFeb 14.2023 — >I had know idea about "await". To learn more I think that is a requirement of an asyn fuction, correct?

That's correct, await can only be used inside an async function. It's major characteristic is that the variable can be used directly below, without having to use a callback.
Copy linkTweet thisAlerts:
@SumeruDigitalSFeb 15.2023 — How to develop a game on roblox

If you're new to game development, we recommend starting with Roblox Studio. It's a great way to get acquainted with the basics of game creation without having to worry about learning a programming language. Once you're comfortable with the basics, you https://sumerudigital.com/how-to-develop-a-game-on-roblox/
×

Success!

Help @phagen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...