Let's get this clarified first as it does cause some confusion for people new to this area.
- Java is a language to write applications.
Where do I put the script?
There are some hard and fast rules and there are opinions and there is no simple answer!
- Any script you want to run on loading the page, must be in the head section or a file referenced in the head section.
- Other script can be either be in the head section or a file referenced from the head section, or it can be in the body section of your page.
Generally though, it is regarded as best practice to write the script into the head section or a file referenced from there.
Adding the Script to the Head Section
The script is written between two script tags as shown.
Calling the Script
The script is called by events. Examples are clicking a button or hovering the mouse over a graphic. For the moment, the examples will show click events.
Calling the script is explained under each example.
OK there are only two and they are simple! I did say this is my first attempt so please be patient. I hope more will be added. Some will be code written by others but full credit will be given to them where that is the case.
|Dialog box to display on loading the page or clicking a button||
causes it to display the message without any user input.
This will occur in any topic that references the file. If you only want it to occur when specific topics are opened (loaded), then write the alert into a function as below and use an onload event.
If you do not want the dialog box to display unless called by the user, then the alert can be written into a function and linked to a button.
The button calls the script under function call_alert() in the file. The code below is added where required to the html in your topic.
Note the button label is in quotes as it is more than one word. The button label in the next example does not have quotes.
|Popup to display on clicking a button||
This popup only displays when the button is clicked. The size and position of the popup are determined by the script.
The button calls the script under function popup1() in the file. The code below is added where required to the html in your topic.
Which method you use will depend on whether the script and image are required across multiple topics (leave it in the file) or are specific to a topic (add the function to the <head> section).
Note the button label does not need to be in quotes as it is only one word.
In RH, the published popup will include a Show Index link. You can remove that by commenting out some lines in the published popup file. Click the button above to display the popup, right click and select View Source to see the lines commented out.
If you find the information and tutorials on my site save you time figuring it out for yourself and help improve what you produce, please consider making a small donation.
Changes to this page
06 Feb 2017
Topic reviewed. No changes made.
05 Feb 2005
What's covered revised.
10 May 2004
the Script to the Head Section" amended to
|02 Mar 2004||
Calling of an alert dialog added to.