views:

317

answers:

2

I have some JavaScript that will execute within a SharePoint web part. I would like to have a function execute when the window is resized. Normally I would use

<html>
  <body onresize="resizeFunction();">
  </body>
</html>

but in SharePoint things start to get hairy. I have an onload function that I am able to use thusly:

_spBodyOnLoadFunctionNames.push('myFunctionName');

and this gets executed in the onLoad event. Is there something similar for onResize events?

+1  A: 

You might consider using an external javascript library such as jQuery or prototype, etc.

For example, in jQuery, you might try (untested code!!)

$("window").bind("resize", "myFunctionName");

jQuery docs: http://docs.jquery.com/Events/bind

info on using jQuery in SharePoint: http://weblogs.asp.net/jan/archive/2008/11/20/sharepoint-2007-and-jquery-1.aspx

EDIT: If you'd rather not use an external library, on document load run a function that attaches another function to the resize event. Something like:

_spBodyOnLoadFunctionNames.push(function() {
    window.onresize = function() {
         /* resize code here */
    }});

You could of course use named functions.

EDIT 2: Defining your events as above (window.onresize = ) is only good for one event. So if a multiple event handlers are specified the same way for the same event on the same page, whichever one is specified last "wins." The "correct" way to do it is to attach an event handler, and the easiest way to accomplish this is through a library, as they handle cross-browser differences transparently.

Chloraphil
I'd rather not use an external library if I don't have to, but if that's the only way...
Nathan DeWitt
A: 

$(window).resize(function(){ alert("Ya its workingggg") ; });

Abhinandan