In the previous videos, you wrote a program to make an alert box pop up when somebody opens your document.
Then you added HTML formatting to make that message more eye-catching.
Right now, the only way to see the alert is to run the code manually.
A reader would have to open your document, then open the script editor, and run the code to show the welcome message.
That isn’t very easy or welcoming!
In this video, you will add code to make the alert box appear automatically every time someone opens your document.
To do that, you will make an event, also called a trigger.
In computer science, an event is an action that tells code to run.
In this case, you will set up an event that tells the program to run every time your Research Notes document is opened.
To set up an event, change the function at the beginning of the script.
A function is a routine built into the code that tells the computer to perform a specific task.
Make sure you have clicked on the code dot GS tab.
In the first line of the script, after the word function, delete the word myFunction.
Leave the parentheses in place.
Then, type in the words onOpen, without any spaces between them and a capital O in Open.
Then, return to the Research Notes tab, and click refresh to test your code.
If your alert doesn’t open, don’t worry.
An important part of writing code is testing what you’ve built, then figuring out how to fix it if it doesn’t work.
Stick with it!
If you test your code a few times and can’t get it to work, ask your neighbor for help.
When you add something new, the program may ask for authorization to run.
This makes sure the person opening the document knows that code will run.
Once you’ve programmed your alert, share your document with a classmate, so they can see the message you created.
Look in Google drive or in your email for the different versions of Research Notes that your classmates shared with you.
See what kinds of welcoming messages your fellow computer scientists created.
If you want to experiment more with HTML to update the styling of your alert, return to W3 schools, or search the internet for html style tags.
Now, it’s your turn: Create an on open event for the Opening Message project.
Test the code by refreshing the document.
Share the document with a classmate, so they can see your welcome message.
Check out your classmates’ messages.
Then, click the next arrow to wrap up this activity.
- Create an onOpen event for the “Opening Message” project.
- Test the code by refreshing the document.
- Share the document with a classmate.