?
Solved

jQuery: Combine document ready and window resize functions

Posted on 2011-10-20
3
Medium Priority
?
1,997 Views
Last Modified: 2012-05-12
$(document).ready(function() {
 alert('Document Loaded or Window Resized')
});

$(window).resize(function () {
 alert('Document Loaded or Window Resized')
});

Open in new window


Is there a good way to combine these two functions into one function? Something like this?


$(document).ready.OR.$(window).resize (function() {
 alert('Document Loaded or Window Resized')
});

Open in new window

0
Comment
Question by:hankknight
3 Comments
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 1000 total points
ID: 36999606
You can do like this

$(document).ready(function() {
  alert('Document Loaded or Window Resized');
  $(window).resize(function () {
    alert('Document Loaded or Window Resized');
  });
});

0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 1000 total points
ID: 36999638
Yes, you can attach the same function to multiple events
http://stackoverflow.com/questions/2534089/jquery-multiple-events-to-trigger-the-same-function

but in your case, you need to defined it separately only.

But why show "Document Loaded or Window Resized" message when document load is gonna happen only once in a session??
0
 
LVL 43

Expert Comment

by:David S.
ID: 37004696
You could also use a named function like this:
function loadAndResizeEvents() {
 alert('Document Loaded or Window Resized')
}

$(document).ready(loadAndResizeEvents);

$(window).resize(loadAndResizeEvents);

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month16 days, 10 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question