trigger onresize in cross browser compatible manner

懵懂的女人 提交于 2019-12-01 12:56:33

问题


I would like to trigger the onresize event from my C# code behind. I think this can be done with

Page.clientScript.RegisterScriptBlock(this.getType(), "id", "javascript code");

I have tried element.onresize() but it doesnt seem to work in firefox. What is the correct way to trigger an onresize event similar to the following jQuery?

$("body").trigger("resize");

Using jQuery itself isn't an option.


回答1:


This should do the trick, DOM Level 2, no idea whether this works in IE6, quirks mode still has no information on this stuff:

if (document.createEvent) {
    var e = document.createEvent('HTMLEvents');
    e.initEvent('resize', true, false);
    document.body.dispatchEvent(e);

} else if (document.createEventObject) {
    document.body.fireEvent('onresize');
}

Tested in FF, Chrome and Opera.




回答2:


use this $(window).resize(); (tested in FF, chrome, IE8)

// old answer, fails in FF document.body.onresize()



来源:https://stackoverflow.com/questions/3856098/trigger-onresize-in-cross-browser-compatible-manner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!