From: ibizara on
<script type="text/javascript">
function formfocus() {
document.getElementById('search').focus();
}
window.onload = formfocus;
</script>

I'm looking to add ¬

"is there is a element with the id search if so auto focus"

else

"do nothing"

Many Thanks to anyone wishing to help,
ibizara
From: Bart Van der Donck on
ibizara wrote:

> <script type="text/javascript">
>    function formfocus() {
>       document.getElementById('search').focus();
>    }
>    window.onload = formfocus;
> </script>
>
> I'm looking to add
> "is there is a element with the id search if so auto focus"
> else
> "do nothing"

<body onLoad="
if (document.getElementById('search'))
document.getElementById('search').focus();
">

Hope this helps,

--
Bart
From: AKS on
On 21 ÄÅË, 21:08, Bart Van der Donck <b...(a)nijlen.com> wrote:

>
> š <body onLoad="
> š š š š š š š š šif (document.getElementById('search'))
> š š š š š š š š šdocument.getElementById('search').focus();
> š š š š š š š š">
>
> Hope this helps,

It's not enough. In IE there can be an error. Check this:

<body onload="
if (document.getElementById('search'))
document.getElementById('search').focus();
">

<input type='text' id='search' style='visibility:hidden;'>

From: Thomas 'PointedEars' Lahn on
AKS wrote:
> On 21 дек, 21:08, Bart Van der Donck <b...(a)nijlen.com> wrote:
>> <body onLoad="
>> if (document.getElementById('search'))
>> document.getElementById('search').focus();
>> ">
>>
>> Hope this helps,
>
> It's not enough. In IE there can be an error. Check this:
>
> <body onload="
> if (document.getElementById('search'))
> document.getElementById('search').focus();
> ">
>
> <input type='text' id='search' style='visibility:hidden;'>

The logical course of action should be clear then:

<script type="text/javascript">
function isMethod(o, p)
{
return /\b(function|object)\b/i.test(o[p]) && o[p];
}

function focusElementById()
{
var o = document.getElementById("search");
if (o && isMethod(o, "focus")
&& typeof o.style == "undefined"
|| typeof o.style.visibility != "undefined")
{
o.focus();
}
}
</script>
</head>

<body onload="focusElementById('search');">
...
</body>

More feature-testing might be indicated. One might also want to employ
exception handling (try...catch) in case there are further conditions that
have to be met in order for the element to receive the focus which cannot be
tested for.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Thomas 'PointedEars' Lahn on
Thomas 'PointedEars' Lahn wrote:
> return /\b(function|object)\b/i.test(o[p]) && o[p];

return /\b(function|object)\b/i.test(typeof o[p]) && o[p];
 |  Next  |  Last
Pages: 1 2 3 4 5 6
Prev: print(), iframe, IE 5/6
Next: Request.Form in Javascript