Forum Moderators: open

Message Too Old, No Replies

Form focus script - I screwed something up!

It was working and now it's not!

         

JAB Creations

1:52 am on Nov 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This script WAS working and now it's not. I'm not sure what the heck I did but I am sure it was stupid. Anyway can anyone point it out? I may have accidentally erased the original script! Anyway here is what I have...

<html>
<head>
<title></title>

<script type="text/javascript">
function addEvent(obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}

window.onload = findSearch();
</script>
</head>

<body>

<form action="" class="search" id="search" method="post">
<fieldset>
<input type="hidden" name="path" value="" />
<input type="hidden" name="refine" value="0" />
<input type="hidden" name="result_page" value="" />
<label class="search" for="query_string">Search Site</label>
<input id="query_string" maxlength="50" name="query_string" size="9" onfocus="javascript:if(this.value=='Search...') {this.value='';}" onblur="javascript:if(this.value=='') {this.value='Search...'}" tabindex="1" type="text" value="Search..." />
<input class="button" type="submit" value="Search" />
</fieldset>
</form>

</body>

kaled

10:56 am on Nov 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try removing the javascript: keyword from both event handlers. It is for use with href only.

Kaled.

JAB Creations

5:51 pm on Nov 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The HTML and JavaScript in the body element is just fine...

It's the JavaScript at the top that I am having problems. In trying to converge this and another script I think I accidentally deleted a section!

Anyway the script is supposed to make the search input focus onload and it worked in Gecko/IE/Opera.

JAB Creations

6:23 pm on Nov 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, I re-figured it out! With a doctype and XML declaration I think this correctly rendered and validated as XHTML 1.1.... anyway it works in IE (text/html), Gecko, and Opera! :)

<html>
<head>
<title>Test 2</title>
<script type="text/javascript">
function addEvent(obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}

function focus()
{
addEvent(window, 'load', function() { document.getElementById('query_string').focus()});
}

window.onload = focus();

</script>
</head>

<body>
<form action="" class="search" id="search" method="post">
<fieldset>
<input type="hidden" name="path" value="" />
<input type="hidden" name="refine" value="0" />
<input type="hidden" name="result_page" value="" />
<label class="search" for="query_string">Search Site</label>
<input id="query_string" maxlength="50" name="query_string" size="9" onfocus="javascript:if(this.value=='Search...') {this.value='';}" onblur="javascript:if(this.value=='') {this.value='Search...'}" tabindex="1" type="text" value="Search..." />
<input class="button" type="submit" value="Search" />
</fieldset>
</form>

</body>
</html>