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

The goal of this script is:

  1. to prompt an end user to enter his name the first time it runs the publication.

  2. to store it into a persistent global variable.

  3. to display it in an HTML page.

Steps

We will name our global variable "TheUserName".

First step: write the script

go 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
// 1) 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; 

Second step: display the name in an HTML page

If you use an HTML Viewer publication, then put this HTML code where you want to display the name:

<!--#globalvar ID="TheUserName" -->

If you use an IE Browser publication, then use this JavaScript code:

document.write(window.external.GetGlobalVariable('TheUserName', ''));

Notes

You can of course customize this example: instead of using a dialog box, you could display an HTML page at startup.

img Introduction to Scripting

img Using the Script Manager

img Script Function Reference


About

This is the online documentation of HTML Executable.

About HTML Executable

HTML Executable is a versatile HTML compiler and ebook compiler: it lets you create secure ebooks and desktop applications with your websites, HTML or PDF documents.

You can easily create attractive ebooks, full-featured HTML applications (RIA) and software, digital publications from your websites, PDF files and HTML documents for online or offline distribution.

Learn more - Free Trial