Forum Moderators: open

Message Too Old, No Replies

Moving internal body script to external JS file?

It's a focus onload but script must be completely external...

         

JAB Creations

4:57 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First off here is the full working code...
<form action="" id="search" method="post">
<div>
<input id="search" maxlength="50" size="9" tabindex="1" type="text" value="" />
<input class="button" type="submit" value="Search" />
</div>
</form>

<script language="JavaScript">
<!--
document.getElementById('query_string').focus();
document.search.query_string.focus();
//-->
</script>

Now there are two lines that both work in Gecko/IE/Opera. I put both in just so we had more then one option.

Now what I'm trying to do is get this script out of the body completely. I have a <script src="" in the head from which I want this to be able to work from. I don't want to use the body element with any javascript attributes either.

So can someone help me get this working? :)

JAB Creations

6:53 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Answered my own question, well kinda...

There is a decent tutorial by Simon Willison at this site...
[sitepoint.com...]

I ended up using the more advanced onload script towards the bottom of page one and it works like a charm. IE 5.01,5.5,6.0, Opera 7.54.,8.5,9.0, and Gecko 1.8.

Bernard Marx

6:53 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have two elements with id="search", and none with "query_string" - but we'll imagine that that's corrected:

onload = function()
{
document.getElementById('query_string').focus();
}

Bernard Marx

6:59 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Personally, I'd prefer a page that uses a small script block after the form. Not very "separation", I know, but sites that use the "onload" event can become irritating. Often, I will be filling in the textbox long before the page loads, and then find that my input has been messed about with. Yahoo & Hotmail both suffer from this.