AlexMorley-Finch
AlexMorley-Finch

Reputation: 6965

jQuery.bind and .trigger without jQuery

I want to do the equivalent of this:

$(window).bind('resize', function () {
    myResizeFunction();
}).trigger('resize');

without jquery.

The reason being is. This is the only part of code in my javascript library that uses jquery.

And i see no point in including jquery if im only using this one command.

NOTE: Just to clarify, i want myResizeFunction to run everytime the broswer is resized. A cross-browser solution is prefered!

Thanks:)

Upvotes: 1

Views: 1033

Answers (3)

Andrew D.
Andrew D.

Reputation: 8220

// binding event
if(window.addEventListener)window.addEventListener("resize",myResizeFunction,false);
else if(window.attachEvent)window.attachEvent("onresize",myResizeFunction);
// trigger event handler
myResizeFunction();

It's not full equivalent of jQuery .bind and .trigger, but it's valid main usage of resize event

Upvotes: 1

thirtydot
thirtydot

Reputation: 228192

As far as I know, jQuery is simply using window.onresize.

window.onresize = function() {
    //stuff
};

For your case:

window.onresize = myResizeFunction;
myResizeFunction();

Upvotes: 2

hungryMind
hungryMind

Reputation: 7009

Simply use this

<script>
var resizeMe = function() {
    myResizeFunction();
}

resizeMe();
</script>

To hook with resize event of window

window.onresize = resizeMe

Upvotes: 2

Related Questions