Ok, this project keeps getting more and more complex. . .
I'm having trouble coming up with a way to do the following in javascript:
I have two lists of varchar strings, list A and list B. Each list is of variable, relatively unlimited length. The same for the strings contained in the lists, but the strings ARE limited to the characters allowed in URLs--they are not huge pages of multi-paragraph text or anything like that.
I want to compare every string in list A to every string in list B.
My comparison, if I were to write in in SQL, would be like the following when looking for a "match."
((string1 LIKE '%string2%') OR (string2 LIKE '%string1%'))
As soon as I have a "match" I can stop checking--one match is enough because a match is BAD. No matches is good. (ooh! that hurts my grammar bone!)
I'd really like to do this in Javascript so that the UI is as streamlined as possible and I also think that my server side logic will be simplified by it. I suppose if I HAD to do it on the server side, I could pull it off using Ajax.
Friday, July 25, 2008
Thursday, July 24, 2008
Javascript: problem giving focus in onBlur event handler
So, yesterday I was writing a javascript that would allow me to avoid using Ajax. I have a tool where you create a new item and give that item a name. I don't want you to name that item the same as other items of the same type and I don't want to warn you about that AFTER you've already submitted the rest of the related data.
When the page loads, I use Coldfusion to build a javascript array of the item names already in use in your account. This is how I avoid Ajax here.
When you navigate away from the text field where you should enter the item name, I call my function using the onBlur event handler for the text field. My function basically loops over the array of item names already in use in your account. If the array item matches the value in the field, I alert you to this and give focus() and select() to the field I'm alerting you about.
I didn't use the form's onSubmit event handler intentionally. By using onBlur of the field, I can be very persistent about making you create an item name that is not already in use.
So, here is where the trouble started. While testing, every time I typed a name that was already in use, I got the alert, but the focus() didn't seem to work in Firefox. I did some searching online and found others having similar problems in Firefox. Someone suggested this fix/workaround:
I used it. It worked. This found solution saved me from some obsessive thinking through the night.
NOW, today I created a function for another page that brings focus() to a required field using the form's onSubmit event handler. In my function, I just used the plain old objRequiredField.focus() after my alert and the focus() worked perfectly in Firefox. VERY confusing.
Here's my theory--onBlur doesn't finish "blurring" or removing focus from the object until AFTER the event handler is finished doing anything you've added to it. So, yesterday's problematic onBlur function was running just fine, but it would give focus() based on my script orders and then, when my function was done, the event handler would finish its default behavior--it would drop focus on the original object and bring focus to the clicked object. setTimeout() allows the blur/focus event go ahead while my function waits. Then, after that minuscule delay, my focus() works as I intend.
Shwew--I'm glad I didn't figure that out in my sleep last night!!
When the page loads, I use Coldfusion to build a javascript array of the item names already in use in your account. This is how I avoid Ajax here.
When you navigate away from the text field where you should enter the item name, I call my function using the onBlur event handler for the text field. My function basically loops over the array of item names already in use in your account. If the array item matches the value in the field, I alert you to this and give focus() and select() to the field I'm alerting you about.
I didn't use the form's onSubmit event handler intentionally. By using onBlur of the field, I can be very persistent about making you create an item name that is not already in use.
So, here is where the trouble started. While testing, every time I typed a name that was already in use, I got the alert, but the focus() didn't seem to work in Firefox. I did some searching online and found others having similar problems in Firefox. Someone suggested this fix/workaround:
setTimeout(function() {fieldObj.focus();}, 0)
I used it. It worked. This found solution saved me from some obsessive thinking through the night.
NOW, today I created a function for another page that brings focus() to a required field using the form's onSubmit event handler. In my function, I just used the plain old objRequiredField.focus() after my alert and the focus() worked perfectly in Firefox. VERY confusing.
Here's my theory--onBlur doesn't finish "blurring" or removing focus from the object until AFTER the event handler is finished doing anything you've added to it. So, yesterday's problematic onBlur function was running just fine, but it would give focus() based on my script orders and then, when my function was done, the event handler would finish its default behavior--it would drop focus on the original object and bring focus to the clicked object. setTimeout() allows the blur/focus event go ahead while my function waits. Then, after that minuscule delay, my focus() works as I intend.
Shwew--I'm glad I didn't figure that out in my sleep last night!!
Subscribe to:
Posts (Atom)