Executing JavaScript using window.onload

This forum is for all Flare issues related to the HTML5, WebHelp, WebHelp Plus, and Adobe Air Targets
Post Reply
FrankyT
Propeller Head
Posts: 56
Joined: Wed Apr 04, 2012 3:45 am
Location: United Kingdom

Executing JavaScript using window.onload

Post by FrankyT »

I'm puzzled by an apparent discrepancy in the way JavaScript is called in WebHelp and HTML5 help using the window.onload command.

I've written a JavaScript function that reformats the main toolbar when the help file is loaded. I first incorporated this function into HTML5 Help (successfully) and am now trying to get it to work in WebHelp. But I have come up against an apparent discrepancy in the way window.onload works in the two formats.

As you may know, in JavaScript, the value assigned to window.onload must be a function name, not a function call. So you refer to the function without the parentheses. (Including the parentheses would execute a call to the function during assignment, and expect a function as a return value. This is valid but not normally what you want to do.)

So, this works in HTML5 Flare output:

Code: Select all

function talkToMe() {
    alert("Hi.");
}

window.onload = talkToMe;
(For those who don't know, you incorporate JavaScript into HTML5 output by entering it in the "Custom JavaScript" box of [HTML5 skin] > Toolbar.)

However, when I include the exact same code in WebHelp output, the JavaScript is not executed. To get it executed, I have to add parentheses:

Code: Select all

function talkToMe() {
    alert("Hi.");
}

window.onload = talkToMe();
Unfortunately, this leads to an "Webpage error" dialog box in IE8. This makes sense: the function isn't really being executed in the way I want, but merely as a step to arriving at a value for window.onload. Since the function talkToMe doesn't provide a function as its return value, window.onload ends up without a value, and IE complains.

I tried inserting the code in the both the WebHelp Toolbar and the Topic Toolbar JavaScript boxes in Flare.

All I want to know is: why does the correct syntax of window.onload execute for HTML5 Help output, but not for WebHelp output? Has anyone else had this problem?

Thanks.
FrankyT
Propeller Head
Posts: 56
Joined: Wed Apr 04, 2012 3:45 am
Location: United Kingdom

Re: Executing JavaScript using window.onload

Post by FrankyT »

Latest news: there is a basic difference between what works in WebHelp and what works in HTML5, and you have to use the appropriate syntax to call the function.

In WebHelp, you should do a straight call to your function:

Code: Select all

// WebHelp:

function talkToMe() {
    alert("Hi.");
}

talkToMe();
However, this doesn't work in HTML5, where you need to use my first solution and employ a window.onload statement:

Code: Select all

// HTML5:

function talkToMe() {
    alert("Hi.");
}

window.onload = talkToMe;
Post Reply