faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

57 of 83 people (69%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I change the focus to the address bar when a page is opened?
Can I give the Location (or Address) bar focus using JavaScript
how can I prevent IE giving address bar focus if TAB is pressed?

Apr 5th, 2008 19:15
ha mo, Bubba Gump, Juergen Thelen, Suds Malone,


In answer to the first two questions:
The browsers location/address bar is no JavaScript object, i.e. there 
is even no locationbar.focus() or similiar method you could use.
It would be possible, if you could send/emulate keystrokes (like a 
TAB) 
via JavaScript, but this is impossible too, due to the heavy security 
impact, that such a feature would have... ;o)
Sorry, but it simply won't work.
Juergen
----
In answer to the question, "How do I stop the tab 
key from going to the address bar (or google bar)?"  You can use the 
onkeydown event of the controls you want to be able to tab to and use 
a function to send it to the next one.  Here is what I came up with, 
you could probaly do a better job but I wrote this in a few mins:
<html>
<body>
    <form id="form1" name="form1">
    <input id="field0" name="field0" value="tab here" 
        onkeydown="javascript: return trapTab(0);"><br>
    <input id="field1" name="field1" value="and tab here"
        onkeydown="javascript:return trapTab(1);"><br>
    <input id="field3" name="field3" value="but don't tab here"><br>
    </form>
    <script language="javascript">
    function trapTab(tabNum)
    {
        // only do this for tab's
        if (event.keyCode != 9)
        {
            return true;
        }
        else
        {
            // The number of controls that can be tab'ed to,
            // doesn't need a var, but for clarity's sake...
            var maxTab = 2;
            // find the next tab control
            tabNum++;
            if (tabNum == maxTab)
            {    tabNum = 0;    }
            // set the focus to the next control in the tab order
            switch(tabNum)
            {
                case 0:
                    document.all("field0").focus();
                    break;
                case 1:
                    document.all("field1").focus();
                    break;
                default:
                    document.all("field0").focus();
                    break;
            }
            // cancel the event since we handled it
            event.cancelBubble = true;
            return false;
        }
    }
    </script>
</body>
</html>
DJ Dr. Evil
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com