The plot thickens... what is the chance I an use such code on page that is dynamically generated?
Details: Client hired an e-cart, wants changes. But limited access to code- i.e. via code templates. I can code one header for all pages, one footer, one body tag, etc. and I change the in-between code for specific pages.
The page content in question is dynamically repeated for the number of products the client has to list (i.e. I make one block of code to present all products).
The specific text box (labled 'COLOR-CODE') to be refocused with every load is in the 4th product down the list and it too is dynamically generated. I know its name from doing a 'view source' on the generated page- i.e "option|Col or-Code|4".
It sounds like the easiest solution will be to hand-code the focus() call into each page. Not all pages are going to have a form, and the element you want to have focused is likely to change depending on the page.
Alrightey. Now that I've actually looked at the source code (:P), here's some goodies:
Unrelated, but in your scripts1.js file, you have <script> tags, which are unnecessary (and in fact generate errors) in .js files.
You have no element whose id is 'option|Color-Code|4'. You have one element whose name is 'option|Color-Code|4', but that's not the same thing. You'd need to specifically set the element's ID. If you absolutely have no choice and must use name instead of ID, try calling this instead:
Your code is attempting to call the 'option|Color-Code|4' focuser once for every product on the page. This is bad because there are situations where there is no element with ID/name 'option|Color-Code|4' when some of those statements are executed, and in some browsers, this means that your JavaScript will stop getting executed.
Ok, so it's a template; this is understandable. If it's possible to put this code somewhere else in your content so that it goes only on that page, but doesn't end up in the product listings, this would be preferable. Otherwise, you'll need to change your code slightly to only call focus() if the element exists:
This code will check for a global variable named elementFocused. If it is unset, then the the script will attempt to find the element whose ID (or name, depending on which set you use) is 'option|Color-Code|4'.
If the element doesn't exist, elementFocused will be set to null, which evaluates to false, so we won't try to focus the element. And the next time we reach this set of statements, elementFocused will still be null (false), so we'll try to focus the element again.
If the element exists, we'll focus it, and then the next time we try to focus the element, elementFocused will be a reference to an element, which evaluates to true, so we'll skip trying to focus the element (since it's already been focused).
Note that in the second set, we have to additionally make sure that there are any elements named 'option|Color-Code|4' before we try to get the 0th one.
Apparently, the cart's response (when "Add to Cart" is pressed) is not enough to trigger the focus again. I have to 'View Cart' or another page. Then when I return to products, the text box gets focus again.
Also, doesn't matter where I place it- i.e. in footer or inline on page.
So how do I create a trigger to focus on this text on page refresh (or whatever is happening after cart's code responds.)?
Well, it will only occur when you tell it to. JavaScript is an event-driven language and can only respond to events, or be called when manually invoked. If you want it to focus, you have to tell it to focus. That's as simple as it gets.
Comment