Why is my javascript function sometimes not working
<p>I have a large javascript function init() that hides and shows a large number of div's. When the page is loaded the javascript function is triggered by the script below. </p>

<pre><code>&lt;script type="text/javascript"&gt;
$(document).ready(function() {
init();
});
&lt;/script&gt;
</code></pre>

<p>Most of the time the javascript is triggered by the script below. However once in a while the script init() seems not to be triggered. Approximately 1 of 20 times the function init() seems not to be triggered. All the other 19 times the page and javascript is working fine without any errors. The circumstances when this error occurs seem to be no different from normally as this error occurs when Pressing F5 20 times. </p>

<p>I checked that init() seems to not being called at all because the first statement (alert("function called");) in init() wasn't called. In the cases that the javascript was working fine I got the alert message and the page was loaded fine without any errors.</p>

<p>I tried different ways to trigger the javascript such as window.onload and putting at the end of the page this script:</p>

<pre><code>&lt;script type="text/javascript"&gt;init();&lt;/script&gt;
</code></pre>

<p>All those different methods to trigger init() didn't seem to make any difference. </p>

<p>This is the init() function:</p>

<pre><code>function init(){
pauzereload__gl_var=1;
load2__gl_var=0;

//Hide bus options:
var boxbusnotshow = document.getElementById("boxDiv").getElementsByTagName("a");
boxbusnotshow[3].style.display="none";
boxbusnotshow[4].style.display="none";
boxbusnotshow[5].style.display="none";

document.getElementById("loadingdiv").style.display = "none";
document.getElementById("loadingdivdate").style.display = "none";
getvariables(); //Get variables generated by the php script, to get activetab

var a=1; b=1;
highlightboxinitiate(a,activetab__gl_var,load2__gl_var);
highlightlistinitiate(a,b);

pauzereload__gl_var=0;
}
</code></pre>

<p>This is the highlightboxinitiate function that is being called from the init() function:</p>

<pre><code>function highlightboxinitiate(divnr,linenr,load2__gl_var) {

getvariables();

for (i = 1; i &lt; 8; i++) {document.getElementById("nextnr" + i).style.display = "none";}
if(counttab__gl_var[linenr]&gt;(9 + settab__gl_var[linenr]) &amp;&amp; load2__gl_var==0){
document.getElementById("nextnr" + linenr).style.display = "block";}

for (i = 1; i &lt; 8; i++) {document.getElementById("prevnr" + i).style.display = "none";}
if(settab__gl_var[linenr] &gt;2 &amp;&amp; load2__gl_var==0){
document.getElementById("prevnr" + linenr).style.display = "block";}

for (i = 1; i &lt; 8; i++) {document.getElementById("pagenr" + i).style.display = "none";}
if(load2__gl_var==0 &amp;&amp; counttab__gl_var[linenr]&gt;9){
document.getElementById("pagenr" + linenr).style.display = "block";}


if(load2__gl_var==1){
for (i = 1; i &lt; 8; i++) {document.getElementById("renextnr" + i).style.display = "none";}
if(counttab__gl_var[linenr]&gt;(9 + settab__gl_var[linenr])){
document.getElementById("renextnr" + linenr).style.display = "block";}

for (i = 1; i &lt; 8; i++) {document.getElementById("reprevnr" + i).style.display = "none";}
if(settab__gl_var[linenr] &gt; 2){
document.getElementById("reprevnr" + linenr).style.display = "block";}

for (i = 1; i &lt; 8; i++) {document.getElementById("repagenr" + i).style.display = "none";}
if(counttab__gl_var[linenr]&gt;9){
document.getElementById("repagenr" + linenr).style.display = "block";}
}

var box = document.getElementById("boxDiv").getElementsByTagName("a");
current_tab__gl_var=linenr;
   for (i = 0; i &lt; box.length; i++) {unselectboxinitiate(divnr,i);}

tabselnr = linenr-1;
if (tabselnr&lt;=2){box[tabselnr].className="selected1";}
if (tabselnr&gt;2 &amp;&amp; tabselnr&lt;=5){box[tabselnr].className="selected2";}
if (tabselnr==6){box[tabselnr].className="selected3";}


    for (j = 1; j &lt; 10; j++) {
    for (i = 1; i &lt; 8; i++) {
    document.getElementById("list"+j+"n"+i).style.display = "none";
    document.getElementById("it"+j+"n"+i).style.display = "none";}
    document.getElementById("list"+j+"n"+linenr).style.display = "block";
    document.getElementById("it"+1+"n"+linenr).style.display = "block";}

}
</code></pre>

<p>This is the highlightlistinitiate function that is being called from the init() function:</p>

<pre><code>function highlightlistinitiate(divnr,linenr) {
   var box = document.getElementById("listmenu").getElementsByTagName("a");
current_list__gl_var=linenr;
   for (i = 0; i &lt; box.length; i++) {
    box[i].className="unselected";}
    box[linenr-1].className="selected";
        for (j = 1; j &lt; 10; j++) {
    for (i = 1; i &lt; 8; i++) {
    document.getElementById("it"+j+"n"+i).style.display = "none";}}
    document.getElementById("it"+current_list__gl_var+"n"+current_tab__gl_var).style.display = "block";
    }
</code></pre>

<p>This is the getvariables function that is being called from the highlightboxinitiate function:</p>

<pre><code>function getvariables(){
counttab__gl_var = new Array (7);
settab__gl_var = new Array (7);
counttab__gl_var [1] = 83; settab__gl_var [1] = 0; activetab__gl_var = 2; counttab__gl_var [2] = 111; settab__gl_var [2] = 0; activetab__gl_var = 2; counttab__gl_var [3] = 137; settab__gl_var [3] = 0; activetab__gl_var = 2; counttab__gl_var [4] = 1; settab__gl_var [4] = 0; activetab__gl_var = 2; counttab__gl_var [5] = 1; settab__gl_var [5] = 0; activetab__gl_var = 2; counttab__gl_var [6] = 1; settab__gl_var [6] = 0; activetab__gl_var = 2; counttab__gl_var [7] = 1; settab__gl_var [7] = 0; activetab__gl_var = 2; }
</code></pre>

<p>I have the impression that the functions are ok because when the page was not properly initiated the first statement in the init() function was not executed. </p>

<p>I checked that crossbrowser is not influencing this problem. This problem occurs in all browsers.   </p>

<p>When the error occurs I don't see any error messages in the firebug console.</p>

<p>I've been searching and debugging a lot so any help will be very welcome and appreciated!</p>
