Javascript onload event misbehaving in WebBrowser control

Status
Not open for further replies.

GohanIYIan

Ars Legatus Legionis
10,196
This is driving me a bit crazy. I've got a small bit of javascript that runs via body.onload and selects a form element. This works flawlessly in IE when run as a standalone browser, but does not work in a WebBrowser control embedded in a VB form. Javascript is as follows:

Code:
function setSelected(num){
	if (document.getElementById('ddQKselect')){
		document.getElementById('ddQKselect').focus();
	}

	var row = Math.floor((num-1) / 3);
	var id = 0;
	if (row == 2) id = num - 7;
	else if (row == 1) id = num - 1;
	else if (row == 0) id = num + 5;
	if (document.getElementById('qkDiv'+id)){
		document.getElementById('qkDiv'+selectedId).style.border='0';
		document.getElementById('qkDiv'+id).style.border='solid 3px #004080';
		document.getElementById('qkButton'+id).focus();
		selectedId = id;
	}
}

Now, if I add a completely pointless alert to the function, it suddenly works. i.e.,
Code:
function setSelected(num){
        alert('wtf');
	if (document.getElementById('ddQKselect')){
		document.getElementById('ddQKselect').focus();
	}

	var row = Math.floor((num-1) / 3);
	var id = 0;
	if (row == 2) id = num - 7;
	else if (row == 1) id = num - 1;
	else if (row == 0) id = num + 5;
	if (document.getElementById('qkDiv'+id)){
		document.getElementById('qkDiv'+selectedId).style.border='0';
		document.getElementById('qkDiv'+id).style.border='solid 3px #004080';
		document.getElementById('qkButton'+id).focus();
		selectedId = id;
	}
}

It does not seem to be a caching issue. Other changes (variable assignments, bits of math, etc) don't produce the same result. Right-clicking the frame to refresh also triggers the event properly even though the first load does not.

There has to be a reason for this. The behavior is the same in ancient versions (IE6+Win2k) and new ones (IE9+Win7). But I'm at a bit of a loss.
 
Status
Not open for further replies.