3

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!

JD Isaacks
  • 56,088
  • 93
  • 276
  • 422

4 Answers4

8
<script>
window.onload = function() {document.getElementById('WeddingBandBuilder').focus()};
</script>
SpliFF
  • 38,186
  • 16
  • 91
  • 120
  • 1
    I'd recommend using an addLoadEvent type structure, to avoid interfering with existing code. http://simonwillison.net/2004/May/26/addLoadEvent/ – Josh Stodola May 21 '09 at 19:45
0

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.

Community
  • 1
  • 1
Grant Wagner
  • 25,263
  • 7
  • 54
  • 64
-1

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(); });
pilsetnieks
  • 10,330
  • 12
  • 48
  • 60
  • 5
    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. – Bjorn May 21 '09 at 14:56
  • @apphacker 1. 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. – pilsetnieks May 21 '09 at 15:08
  • @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 – JD Isaacks May 21 '09 at 15:22
  • 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. – JD Isaacks May 21 '09 at 15:23
  • 1
    @john Isaacks, yes using a JavaScript library in the middle of an existing site can cause problems. – Bjorn May 21 '09 at 17:03
  • 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. – Bjorn May 21 '09 at 17:05
-2

I might check if that page has already included jQuery? If so, you can do something like this:

$(document).ready(function() {
    $('#WeddingBandBuilder').focus();
});
JerSchneid
  • 5,817
  • 4
  • 34
  • 37