最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call onresize from ASP.NET content page - Stack Overflow

programmeradmin7浏览0评论

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.

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.

Share Improve this question edited Feb 8, 2016 at 14:48 Alexander Elgin 6,9954 gold badges42 silver badges52 bronze badges asked Aug 28, 2008 at 20:24 Kevin GriffinKevin Griffin 2,2574 gold badges25 silver badges27 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Place the following in your content page:

<script type="text/javascript">

// here is a cross-browser patible 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.

I had the same problem and have e 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

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!

发布评论

评论列表(0)

  1. 暂无评论