0

I've got a calculation script that executes when the page loads, but also when the window gets resized. The code in between is exactly the same; not re-used.

So I was wondering, not if, but how do I re-use Javascript code?

I've tried a few stuff, like the next piece, but that ain't working.

$(function reUse() {
    alert('Either the page got resized or the page loaded!');
    console.log('Either the page got resized or the page loaded!');
});

$(window).resize(function() {
    reUse();
});

So what should be the correct way of formatting?

4 Answers 4

2

You should put the function outside of the closure. You are now adding the function as a jQuery object. The way to go would be:

var reUse = function() {
    alert('Either the page got resized or the page loaded!');
    console.log('Either the page got resized or the page loaded!');
};

$(window).resize(function() {
    reUse();
});

And you'll want to wrap it all in a closure:

jQuery(document).ready(function($) {
    // the code above
});
Sign up to request clarification or add additional context in comments.

2 Comments

I assume that also applies for the actual code; write it as a variable and re-use that in the functions where it's needed? -> pastebin.com/MaLYCMc9 - Thanks! - And second! Why 'jQuery(document).ready(function($)' instead of $(document).ready(function() {}); - Which seems shorter?
yup, looks good! in javascript you can put everything in a variable: a primitive value, object or function. So anything you'd like to use again, put it in a variable!
2

You're missing a few parenthesis etc, and you can just trigger event handlers

$(function() {
    function reUse() {
        alert('Either the page got resized or the page loaded!');
        console.log('Either the page got resized or the page loaded!');
    }

    $(window).on('resize', reUse).trigger('resize');
});

2 Comments

Why "$(window).on('resize', reUse).trigger('resize');" instead of $(window).resize(function() { reUse(); }); ? Using .trigger() seems a little overdone, since .resize() is enough?
@SanderSchaeffer - It's to trigger the function both on pageload and on resize
1

Write it like this

function reUse() {
    alert('Either the page got resized or the page loaded!');
    console.log('Either the page got resized or the page loaded!');
}

$(window).resize(reUse);

2 Comments

Why would the asker's version fail, though?
Because of the visibility of the reUse function, please read stackoverflow.com/questions/500431/javascript-variable-scope
1

try to make it global, in your scope:

var reUse;
$(reUse = function reUse() {
    alert('Either the page got resized or the page loaded!');
    console.log('Either the page got resized or the page loaded!');
});

$(window).resize(function() {
    reUse();
});

or for the second use, if you don't have any special parameter to pass to your function:

$(window).resize(reUse);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.