|
home / faq / faq 14
Frequently Asked Question #14
Question: What is this HM_f_DoNothing code, and why
must I insert it at the top of each of my configuration files?
Answer: As explained as part of
step 2 of the setup instructions,
every configuration file must begin with the following block of code:
function HM_f_DoNothing() {return;}
if(typeof(HM_f_UpdateDefaults)=="undefined") {
HM_f_UpdateDefaults=HM_f_DoNothing;
HM_f_SetMenuTemplate=HM_f_DoNothing;
HM_f_SetMenus=HM_f_DoNothing;
HM_f_SetItems=HM_f_DoNothing;
}
And while this step is covered in the setup instructions, the full reason why
this code is necessary is not. That we'll provide now.
When loading the HierMenus scripts into the page, we use a two step process. First,
HM_Loader.js is loaded into the page, by way of the standard HTML tag that you
must insert into each of your HierMenus pages:
<script type="text/javascript"
language="JavaScript1.2"
src="/hm/scripts/HM_Loader.js">
</script>
Within HM_Loader.js, two (or more) files are additionally loaded into the
page using standard document.write JavaScript statements. When loading scripts
into a page via this method, however, each of the two latest major browsers (Internet
Explorer and Mozilla/Gecko) can process the scripts slightly differently than they
would if the scripts were loaded directly within the page (as HM_Loader.js
is) or if the scripts were simply embedded within the page. The key difference is this:
in both browsers, when loading multiple scripts into a page via document.write
statements (as we do in HierMenus), if the loading process of the page is stopped
(via the user clicking the stop button or the user clicking through to a new page)
there is the possibility that the browser will actually parse and execute one of
the later external scripts--even though a prior script never finished
loading--just before the page is actually unloaded. In the case of HierMenus, our
second script--namely, your configuration file--relies on routines that
are defined within the first script loaded--namely the HierMenus execution scripts.
If the second JavaScript is parsed without the benefit of having the first JavaScript
already parsed and executed, the results will be errors as the configuration file commands
attempt to execute functions that, as far as the browser is concerned, don't actually
exist.
Presumably, this later-before-former JavaScript parsing only happens when the
two scripts in question are of significantly different sizes. When scripts are loaded
into a page via document.write commands, most browsers can make the overall
download of the page more efficient by retrieving both scripts simultaneously, in the
same manner that multiple images of a page can be retrieved simultaneously. Though
multiple retrieval requests are made for the scripts on the page, the actual processing
of the scripts (and parsing of any further HTML) is delayed, so that the
scripts can be executed in the proper order. In this one case, however, if the retrieval
of the first script is interrupted before it is completed but after the retrieval
of the second script is complete then the second script can be quickly parsed and
executed after the stop button is clicked or just before the current page is unloaded and
the new page is loaded.
There are many ways to work around this problem, and we've chosen a way that we
thought would be the easiest to explain and implement for all HierMenus configurations.
Unfortunately, it's impossible for us to fix the problem using code within the
HM_Loader.js or the execution scripts; since the fact is, at the point the problem
occurs it's possible that neither of them will be available to the configuration file
commands. (Gecko, for example, will occasionally throw an error at a point after
the HM_Loader.js code has already been removed from the page.) Basically, the
HM_f_DoNothing code above provides "dummy" function declarations for each of the
main commands in the configuration file if and only if they are not already
declared. In this way, if the execution scripts never finished loading, the commands in
the configuration file will still be executed, but they will simply "do nothing." No menus
will be created (as was the user's intention, since they clicked the stop button), but
no errors will be generated, either.
Of course, if you would like to implement your own checks based on the information
above, you are welcome to do so.
|