JS: tag load equivalent of tag?

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 make an equivalent with a script in <head> or something?

Thanks!

+3
source share
4 answers
<script>
window.onload = function() {document.getElementById('WeddingBandBuilder').focus()};
</script>
+8
source

You should always be careful if not defined window.onload. I was burned several times, suggesting that I would only bind things to <body onload="...">or window.onload.

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

0
source

JS-, ​​ jQuery Prototype, script - :

jQuery:

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

:

Event.observe(window, 'load', function() { $('WeddingBandBuilder').focus(); });
-1

Can I check if this page is included in jQuery? If so, you can do something like this:

$(document).ready(function() {
    $('#WeddingBandBuilder').focus();
});
-2
source

Source: https://habr.com/ru/post/1708819/


All Articles