By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,363 Members | 2,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,363 IT Pros & Developers. It's quick & easy.

execScript focus problem (IE specific)

P: 2
I'm using an Ajax call to open a search box in the page and I want the focus to go to the box automatically.

So, in the httpObject.responseText, I tried sending the following in addition to the search box markup:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. document.getElementById('Message').focus();
  3. </script>
and, after writing the response using innerHTML, the originating Ajax function calls an eval function to enable the JavaScript, the critical line being:

Expand|Select|Wrap|Line Numbers
  1. if(script) (window.execScript) ? window.execScript(script) : window.setTimeout(script, 0);
It works in Firefox and Safari because they default to the window.setTimeout method. The problem is (as usual) Internet Explorer...

1. The JavaScript executes just fine - the focus goes to the search box - if I open the code of the search box directly as a page. I tried other ways of setting the focus which worked similarly but also failed similarly when using window.execScript.

2. I've tried other things in place of focus() and they all worked. A simple alert works, writing text to the box works but the focus is never set.

3. I found that if I included an alert in the eval function, the focus WOULD be set after I clicked OK. But that's clearly defeating the purpose of saving the user a click.

I doesn't actually bother me that IE users would get an inferior experience but this is such a curious problem I really want to know the answer.

Are there any IE experts here that can tell me?
Nov 6 '08 #1
Share this Question
Share on Google+
1 Reply

P: 2
I pressed on with my coding and I've just noticed that focus is now working in IE.

I've no idea what was wrong before.
Nov 6 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.