tags:

views:

478

answers:

4

I need to basically add this to my page:

<body onload="document.getElementById('WeddingBandBuilder').focus()">

However due to my template I cannot change the tag. So is there a way to do the equivalent with a script in the < head > tag or something?

Thanks!

+1  A: 

Use a JS library like jQuery or Prototype and create an external script file with something like this:

for jQuery:

$(function() { $('#WeddingBandBuilder').focus(); });

for Prototype:

Event.observe(window, 'load', function() { $('WeddingBandBuilder').focus(); });
Nouveau
Did he say he was using a JavaScript library? No. I'm so tired of JavaScript questions getting library answers when they didn't ask for it. If someone asks for a question in Python do people just drop in some Django crap? Also if he can't edit the body tag what makes you think he can add a JavaScript library and why would he want to add one just so he can do this tiny thing.
apphacker
@apphacker1. He said he can edit the head tag.2. Python/Django don't quite compare to this situation because a big part of the appeal of these libraries is that they bring some sanity into browser differences.3. window.onload = whatever; in the head might easily break, using a library won't.
Nouveau
@nouveau, I don't know much about JS, I am mainly a Actionscript and PHP programmer. But this site is a pretty decent size ecommerce site, is it possible that adding jquery could effect anything in a negative way? Thanks
John Isaacks
I tried window.onload = whatever; but It didn't work. The reason I need this is because a flex app wont show in IE until it is moused over...setting focus is a work around.
John Isaacks
@john Isaacks, yes using a JavaScript library in the middle of an existing site can cause problems.
apphacker
@Nouveau, wish I could downvote your comment too.
apphacker
And just for the record I'm a big Dojo fan and use Dojo for just about everything, but I don't add Dojo answers for non Dojo questions.
apphacker
A: 

If you want some tangential advice, I would take a few minutes to learn jquery. It's amazing and will make everything you do with javascript easier! Your current problem could be solved like this:

$(document).ready(function() {
    $('#WeddingBandBuilder').focus();
});
JerSchneid
+7  A: 
<script>
window.onload = function() {document.getElementById('WeddingBandBuilder').focus()};
</script>
SpliFF
I'd recommend using an addLoadEvent type structure, to avoid interfering with existing code. http://simonwillison.net/2004/May/26/addLoadEvent/
Josh Stodola
@Josh Excellent link, thank you! That is exactly what I needed!
Adam Tuttle
A: 

You should always be careful there isn't already a window.onload defined. I've been burned a number of times by assuming I would be the only one attaching things to <body onload="..."> or window.onload.

See this answer and comments for a solution to this issue.

Grant Wagner