trigger onresize in cross browser compatible manner

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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

Method 2

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x