Search code examples
asp.netjavascriptmaster-pagesonresize

Call onresize from ASP.NET content page


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.


Solution

  • 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.