Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Sub executes multiple times - focus problem?

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3431
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Sub executes multiple times - focus problem?
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Sub executes multiple times - focus problem? - 6/17/2005 6:28:46 AM   
  adblockfreak

 

Posts: 4
Score: 0
Joined: 6/17/2005
From:
Status: offline
Hi all,
I am new to this forum and would appreciate any help you can give me. I have a form whose fields are validated as you go along, using the onfocusout attribute. This works great, except when a Select input (pulldown menu) is selected immediately after typing in a text-type input. For some reason, the validation subroutine is run multiple times -- 3 times if you clicked on the Select, 2 times if you Tabbed to it. I've figured out which lines seem to cause it, but I don't know why. To show you what I mean, my code is below -- just copy & paste into an html document, and change the function name in "onfocusout" to test it. If it still doesn't make sense, please post back and I'll be glad to clarify.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>TEST</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<form action="" method="post">
<div>
<input type="text" disabled name="counter" value="1">
<input type="text" name="order" id="order" onfocusout="testOriginal('order')">
<select>
<option>abcd</option>
</select>
</div>
</form>

<script type="text/vbscript">
Sub testOriginal(strElement)
If (Len(document.all(strElement).value) <> 0) Then
//This next line would display an error message, but it has been
//changed from the msgbox command - more informative, less annoying
document.all("counter").value = document.all("counter").value + 1
//Removing these next 2 lines causes it to behave normally
document.all(strElement).focus
document.all(strElement).select
Else
document.all("counter").value = 1
End If
End Sub

//behaves normally
Sub test2(strElement)
document.all("counter").value = document.all("counter").value + 1
End Sub

//gives the error
Sub test3(strElement)
document.all("counter").value = document.all("counter").value + 1
document.all(strElement).focus
document.all(strElement).select
End Sub

//gives the error
Sub test4(strElement)
document.all("counter").value = document.all("counter").value + 1
document.all(strElement).focus
End Sub

//gives a similar error - only difference is that in this case, mouse or Tab both cause 2 executions
// (instead of mouse causing 3 like before)
Sub test5(strElement)
document.all("counter").value = document.all("counter").value + 1
document.all(strElement).select
End Sub

</script>


</body>
</html>
 
 
Post #: 1
 
 Re: Sub executes multiple times - focus problem? - 6/17/2005 7:05:09 AM   
  adblockfreak

 

Posts: 4
Score: 0
Joined: 6/17/2005
From:
Status: offline
Update: I replaced the onfocusout attribute with the onblur attribute (http://www.w3.org/TR/html4/index/attributes.html), and that seems to have fixed it. I think that onblur is a standard attribute whereas onfocusout is proprietary. I still don't really know why that worked, but hey, if it fixes it...

(in reply to adblockfreak)
 
 
Post #: 2
 
 Re: Sub executes multiple times - focus problem? - 6/17/2005 7:17:40 AM   
  DragonMasterXX5

 

Posts: 52
Score: 0
Joined: 6/13/2005
From: USA
Status: offline
what's the new script?

(in reply to adblockfreak)
 
 
Post #: 3
 
 Re: Sub executes multiple times - focus problem? - 6/27/2005 2:29:36 AM   
  adblockfreak

 

Posts: 4
Score: 0
Joined: 6/17/2005
From:
Status: offline
I didn't have to change the script, actually. I changed the attribute that calls it, from "onfocusout" to "onblur."

But actually, I'm getting a similar problem again. And this time, using onblur does not fix it. Here is what I am doing:

Here's my script...

Sub ron_mon
If (document.all("ron").value = "" or document.all("mon").value = "") Then
document.all("rmavg").value = ""
document.all("rmavg").disabled = false
End If
If (document.all("ron").value <> "") Then
If IsNumeric(document.all("ron").value) Then
If (CDbl(document.all("ron").value) > 100) OR (CDbl(document.all("ron").value) < 40) Then
MsgBox "The value of RON should be between 40 and 100. Please doublecheck.", 48, "Entry Error"
document.all("ron").Focus
document.all("ron").Select
Exit Sub
End If
Else
MsgBox "The value of RON should be a number between 40 and 100." , 48, "Entry Error"
document.all("ron").Focus
document.all("ron").Select
Exit Sub
End If
End If
If (document.all("mon").value <> "") Then
If IsNumeric(document.all("mon").value) Then
If (CDbl(document.all("mon").value) > 100) OR (CDbl(document.all("mon").value) < 40) Then
MsgBox "The value of MON should be between 40 and 100. Please doublecheck.", 48, "Entry Error"
document.all("mon").Focus
document.all("mon").Select
Exit Sub
End If
Else
MsgBox "The value of MON should be a number between 40 and 100." , 48, "Entry Error"
document.all("mon").Focus
document.all("mon").Select
Exit Sub
End If
End If
If (document.all("ron").value <> "" and document.all("mon").value <> "") Then
document.all("rmavg").value = Round((CDbl(document.all("ron").value) + CDbl(document.all("mon").value))/2,1)
document.all("rmavg").disabled = true
End If
End Sub


... and here is the code:

<td><acronym title="Research Octane Number">RON</acronym></td>
<td>
<input type="text" class="textbox" name="ron" size="10" value="<%=ron%>" onblur="ron_mon()" tabindex="16">
</td>
<td><acronym title="Motor Octane Number">MON</acronym></td>
<td>
<input type="text" class="textbox" name="mon" size="10" value="<%=mon%>" onblur="ron_mon()" tabindex="17">
</td>
<td><acronym title="Road Octane Number">(R + M)/2</acronym></td>
<td>
<input type="text" class="textbox" name="rmavg" size="10" value="<%=rmavg%>45" onblur="ron_mon()" tabindex="18" disabled>
</td>



The behavior now is that if I enter an invalid number in any of these 3 fields, and hit Tab, and the next field is one that would also call ron_mon(), then the sub is run twice (so the error is given twice). My guess is that there is some conflict going on with which field gets the focus, but I'm not sure.

(in reply to adblockfreak)
 
 
Post #: 4
 
 Re: Sub executes multiple times - focus problem? - 6/30/2005 12:11:18 AM   
  adblockfreak

 

Posts: 4
Score: 0
Joined: 6/17/2005
From:
Status: offline
Can anybody at all help me with this?

(in reply to adblockfreak)
 
 
Post #: 5
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Sub executes multiple times - focus problem? Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts