views:

1287

answers:

4

I have a JavaScript method that I need to run on one of my pages, in particular, the onresize event.

However, I don't see how I can set that event from my content page. I wish I could just put it on my master page, but I don't have the need for the method to be called on all pages that use that master page.

Any help would be appreciated.

A: 

have you tried putting it a form tag on the page and putting it in the onresize event such as:

form onresize="MyFunc"

jmein
+2  A: 

Place the following in your content page:

<script type="text/javascript">

// here is a cross-browser compatible way of connecting 
// handlers to events, in case you don't have one
function attachEventHandler(element, eventToHandle, eventHandler) {
    if(element.attachEvent) {
       element.attachEvent(eventToHandle, eventHandler);
    } else if(element.addEventListener) {
       element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false);
    } else {
    element[eventToHandle] = eventHandler;
  }
}

attachEventHandler(window, "onresize", function() {
    // the code you want to run when the browser is resized
});

</script>


That code should give you the basic idea of what you need to do. Hopefully you are using a library that already has code to help you write up event handlers and such.

Jason Bunting
A: 

How about use code like the following in your Content Page (C#)?

Page.ClientScript.RegisterStartupScript(this.GetType(), "resizeMyPage", "window.onresize=function(){ resizeMyPage();}", true);

Thus, you could have a "resizeMyPage" function defined somewhere in the Javascript and it would be run whenever the browser is resized!

Adhip Gupta
A: 

I had the same problem and have come across this post :

IE Resize Bug Revisited

The above code works but IE has a problem where the onresize is triggered when the body tag changes shape. This blog gives an alternate method which works well

Gordon Carpenter-Thompson