Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7098133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:01:03+00:00 2026-05-28T11:01:03+00:00

This works for running the same code on both ready and resize: $(document).ready(function() {

  • 0

This works for running the same code on both ready and resize:

$(document).ready(function() {

    $(window).resize(function() {

         // Stuff in here happens on ready and resize.

    }).resize(); // Trigger resize handlers.       

});//ready

How would you accomplish the same result using jQuery.on() ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:01:03+00:00Added an answer on May 28, 2026 at 11:01 am

    on can be used to wire up the resize and ready events just like any other event.

    So for your case, you could create a function that has the code you want to happen for resize and ready, and then pass it to both calls to on.

    If you want to keep your enclosing scope clean, you could do all this in an immediately executing function:

    (function() {
        function stuffForResizeAndReady(){
           // Stuff in here happens on ready and resize.
        }
    
        $(window).on("resize", stuffForResizeAndReady);
        $(document).on("ready", stuffForResizeAndReady);
    })();
    

    2012-07-25: There are 2 differences to be aware of when using .on() to attach ready handlers:

    • Ready handlers added via $(fn) and $(document).ready(fn) are “retro-fired” while ones added by .on() are not. Using those, if you add a handler after the DOM is already loaded, the fn will be fired immediately. If you add a handler via .on('ready', fn) after the DOM is loaded, it will not be fired by jQuery, but you can manually .trigger('ready') it.

    • When you use $(fn) or $(document).ready(fn) to add a ready handler, the fn receives jQuery as its 1st arg, allowing the familar jQuery(function($){ }) usage. If you use $(document).on('ready', fn), the 1st arg that the fn receives is an event object. In both cases this inside the fn is the document. If you were to do something abnormal like $('#foo').on('ready', fn) for the purpose of triggering, this would be the #foo element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Its been mentioned in other answers about getting the same code running for both
I have been using win32api.MessageBox to do alerts, and this works for apps running
We're currently running an svnserve instance as NT service. While this works, it's needlessly
I have problems running a dynamic LIKE statement in my project: this query works
This MySQL script installs multiple triggers. It works on one machine running MySQL 5.0.51b-community.
This works fine: $.getJSON(http://localhost:59396/xxxWeb/ CarouselHandler.ashx?action=getproducts&ids= + ids, function(data) { carousel.size(allProductIDs.length); if (numberOfImagesLeftToShow < numberOfImagesToDisplay)
The Short and Sweet Running this code in Ruby 1.9: FOO = global constant
Why would I get NoMethodError on my Heroku app when the same code works
I'm running Mysql 5.0.77 and I'm pretty sure this query should work? SELECT *
This one has us all baffled at work. We have two services running on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.