How to associate several component events to one JavaScript file?

Click For Summary

Discussion Overview

The discussion revolves around how to associate multiple buttons on a web page with a single JavaScript file for tone generation, rather than using separate custom code for each button. It explores the use of Google Web Designer and the concept of external JavaScript.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in using a single JavaScript file for multiple buttons and questions the appropriateness of Google Web Designer for this task.
  • Another participant suggests looking into external JavaScript and provides a code snippet for associating a JavaScript file with HTML.
  • A different participant mentions their background in C and C++ and expresses interest in learning HTML and JavaScript based on shared ideas.
  • One participant critiques Google Web Designer as potentially limiting and suggests that simpler HTML/CSS/JS could be more effective for achieving the desired functionality.
  • Another participant shares their confusion regarding musical notes and expresses a preference for a simplified numbering system for notes, linking it to their experience with music.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of Google Web Designer, with some suggesting it may hinder progress while others find it useful for learning. There is no consensus on the best approach to implement the tone-generating functions.

Contextual Notes

Participants mention various resources and approaches, but there are unresolved questions about the best practices for integrating JavaScript with HTML in this context.

Bob Walance
Insights Author
Gold Member
Messages
84
Reaction score
55
TL;DR
Need help understanding associating web page component events to one JavaScript file.
I am creating a web page that will have several buttons on it. When a button is pressed it will play a tone or combination of tones.

With the Google Web Designer application, I can create a simple page with one button on it. I'm able to associate the clicking of that button with "Custom Code" for that button in order to play a tone, and it works.

I have not been able to figure out how to use one JavaScript file that several buttons use for their tone-generating functions - rather than having "Custom Code" for each button separately.

I'm not a software engineer and perhaps Google Web Designer is not the appropriate tool.

Any ideas would be appreciated.
 
Technology news on Phys.org
  • Like
Likes   Reactions: Wrichik Basu and Bob Walance
I can follow your html and javascript in Idea#1, and I'm playing around with it on the codepen website. I will start learning html. The javascript is easier for me because I have written C and C++ applications.

Your Idea#4 is something that I will study.

Thank you very much, pbuk. Your effort is sincerely appreciated.
 
Bob Walance said:
I have not been able to figure out how to use one JavaScript file that several buttons use for their tone-generating functions - rather than having "Custom Code" for each button separately.

I did find exactly what I was looking for by searching for 'html external javascript'. The key concept is 'external javascript'.

This is how to do it --
Place this in the html file. It associates the name of the JavaScript file and its functions ('<' and '>' omitted here):
script src="myMusicFunctions.js" /script
My .js file has a function called playSineWave(freq).

This code was created by the Google Web Designer app for the button press events:
script type="text/javascript" gwd-events="handlers"
window.gwd = window.gwd || {};
gwd.playThe440Hz = function(event) {
playSineWave(440);
};
gwd.button2 = function(event) {
playSineWave(550);
};
gwd.button1event = function(event) {
playSineWave(440);
};
gwd.button2event = function(event) {
playSineWave(550);
};
/script
 
If you are happy with that then fine, but IMHO sooner or later you are going to find that Google Web Designer is just getting in your way and not really doing much for you - you can achieve a lot with just a few lines of vanilla HTML/CSS/JS (leveraging the excellent tone.js library).

 
  • Like
Likes   Reactions: Bob Walance
Yes, GWD is definitely a crutch. I have zero experience with html so it will help me achieve my goal more quickly AND allow me to see what-is-what in the html.

Cb or B#? E# or Fb? That is all very confusing to me - even having played trombone all throughout junior and senior high school. IMHO, the 12 notes should be called 0,1,2,3...11. No more enharmonic equivalents to worry about. No more sharp and flat keys and very simple key changes.

I blame the piano for this obfuscation. I posted about this a couple of years ago and am still really enjoying playing this other layout. There's a video at the bottom of the thread.
https://www.physicsforums.com/threa...d-juxtaposed-with-a-balanced-keyboard.989172/
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
2
Views
6K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 4 ·
Replies
4
Views
4K