Navigation: For Developers > Extend Functionality with HEScript > Sample Scripts >

How to prompt end users for their name once and store it?

 

 

 

 

📚This guide will walk you through a script that accomplishes the following objectives:

 

ØPrompts the end user to enter their name the first time the publication runs.

ØStores the user's name into a persistent global variable.

ØDisplays the user's name in an HTML page.

 

Getting Started

 

We will be using a global variable named "TheUserName".

 

Step 1: Writing the Script ✍️

 

Navigate to the Script Manager, double click on "UserMain" and copy/paste this script in the OnPubLoaded function event:

 

function OnPubLoaded: Boolean;
var S: String;
begin
// Checks whether the user was already prompted.
// If the TheUserName global variable already has a value, then we do not
// prompt the user again.
if GetGlobalVar("TheUserName", "") <> "" then exit;
// Prompts the user then:
S := InputBox("Welcome!"#13#10"Please enter your name:", "What is your name", "");
// If the user does not give a name, set it to "Mysterious user"...
if S = "" then
S := "Mysterious user"
else
begin
// Stores the result only if the user has given a name.
// So he/she will still be prompted the next time.
SetGlobalVar("TheUserName", S, True);
// True means that the global variable is stored.
end;
// When the publication is starting and before the homepage is displayed.
// Set Result to True if you want to exit immediately without any warning.
Result := False;
end;

 

Step 2: Displaying the Name in an HTML Page 🖥️

 

Use this JavaScript code:

 

<script>
function DemoCallback(content) {
    document.write(content);
}    
</script>
<script>
htmlexe.GetGlobalVariable('TheUserName', '', DemoCallback);
</script>
 

 

Additional Notes 📝

 

You can customize this example as per your needs. For instance, instead of using a dialog box, you could display an HTML page at startup.