Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forcing a jQuery ready block to run after all other ready blocks

Tags:

jquery

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?

like image 976
Antony Scott Avatar asked Oct 10 '09 22:10

Antony Scott


People also ask

Can there be more than one ready function in jQuery?

Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.

What does $( function ()) shorthand do?

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").

When using jQuery the ready () function executes after the DOM has been loaded?

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.

How do I call a function after document ready jQuery?

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.


2 Answers

$(window).load() function fires after the $(document).ready()

like image 178
Muhammad Adnan Avatar answered Nov 12 '22 05:11

Muhammad Adnan


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

like image 41
Paul Sweatte Avatar answered Nov 12 '22 05:11

Paul Sweatte