Is it possible to have one jQuery ready block executed after all others have done so.
I have this in my master page ...
<script type="text/javascript">
$(document).ready(function()
{
$("input[type='text']:enabled:first", document.forms[0]).focus().select();
});
</script>
and this in one of my other pages ...
<script type="text/javascript">
$(document).ready(function()
{
$('textarea.message-body').wysiwyg();
});
</script>
The problem I am seeing is that the first block is executed before the second block, which is causing a problem with the tab key. What's happening is that the keyboard focus goes to the address bar in my browser when I press the tab key. I've changed the code slightly to have this in my master page ...
<script type="text/javascript">
$(document).ready(function()
{
var bodies = $('textarea.message-body');
if (bodies.length > 0)
{
bodies.wysiwyg();
}
$("input[type='text']:enabled:first", document.forms[0]).focus().select();
});
</script>
and do away with the other ready block completely. Doing that makes the tab key work properly, and set the focus onto my textarea.
I'd rather not have page specific code in my master page.
So, is there a way to make my textbox focusing code run after the wysiwyg code?
Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.
This is a shortcut for $(document). ready() , which is executed when the browser has finished loading the page (meaning here, "when the DOM is available").
jQuery ready() MethodThe ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.
Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ).on( "load", function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready. // A $( document ).ready() block.
$(window).load()
function fires after the $(document).ready()
Add a custom event trigger to your master page:
<script type="text/javascript">
$(document).ready(function()
{
$('textarea.message-body').wysiwyg();
$(document).trigger('tb');
});
</script>
And bind it to the other page:
<script type="text/javascript">
$(document).bind('tb', function(){
$("input[type='text']:enabled:first", document.forms[0]).focus().select();
});
</script>
References
jQuery Special Events...Virtually, not in Real Life
How to Create Custom Events in JavaScript
AOP Aspect of JavaScript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With