Alert

One of the simplest ways to debug client script is to use the alert functionality. alert simply pops up a message window displaying information you choose to include. This can be useful for showing the values of fields or variables at certain times, or simply to confirm that certain functions were entered.

Let's see how alert works in the following code:

alert('Debug Message');

The preceding script will simply show a pop-up message to the user saying Debug Message. It is a simple line of code to write and provides instant feedback.

Let's have a look at how we can use alert to help us test our code:

function onLoad() {
alert('Start of script');
var shortDescBefore = g_form.getValue('short_description');
alert('The short description beforehand is ' + shortDescBefore);
g_form.setValue('short_description', 'Alerting Issue');
var shortDescAfter = g_form.getValue('short_description');
alert('The short description afterwards is ' + shortDescAfter);

}

In the preceding example, we are using three messages to test that the script is working how we would like it to. The first will simply alert us to the script starting by showing the user Start of script. This can be helpful to check that JavaScript is running correctly on a browser or that other client scripts are not causing an error that stops this script from running.

The second message gives us the value of the short description as the form is loading. We are storing the value of the short description in the shortDescBefore variable and using it in our alert. We can combine strings of text and variables together in an alert by using the plus sign between them.

The third message shows the value of the short description field after we have used the g_form setValue method to amend it. The third message will read The short description afterwards is Alerting Issue.

Using alert allows us to test and also debug script that has been written quickly and effectively. The problem with using alert is that you must ensure all alerts are removed or commented out if being used for tests or debugging. This is due to them being very obvious to users if they are not removed, and some do find them rather irritating to have to keep clicking away.

I tend to like testing in this way for initial development because of how easy it is to use. However, alert does not provide an ongoing testing method, so it can be better to put in a cleaner logging method for maintenance of your code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.114.142