1

I have two nearly identical functions, but I'm not sure how I can refactor this so they can share the same logic.

function Content_chkClick(obj) {
    var frame = $('#iFM')[0];

    if (frame.contentWindow.Content_chkClick) {
        frame.contentWindow.Content_chkClick(obj);
    } else {
        $('.TabContent', frame.contentWindow.document).each(function () {
            var frame = this;
            if (frame.contentWindow.Content_chkClick) {
                frame.contentWindow.Content_chkClick(obj);
            }
        });
    }
}

function Content_invokeClickEvent(id) {
    var frame = $('#iFM')[0];

    if (frame.contentWindow.Content_invokeClickEvent) {
        frame.contentWindow.Content_invokeClickEvent(id);
    } else {
        $('.TabContent', frame.contentWindow.document).each(function () {
            var frame = this;
            if (frame.contentWindow.Content_invokeClickEvent) {
                frame.contentWindow.Content_invokeClickEvent(id);
            }
        });
    }
}

Ultimately, what I want to be able to do is just have something like

function Content_chkClick(obj) {
    someCommonFunction(Content_chkClick, obj);
}
function Content_invokeClickEvent(id) {
    someCommonFunction(Content_invokeClickEvent, id);
}

1 Answer 1

1
function Content_xClick(fname, obj) {
    function callIfPossible(frame) { 
        if (frame.contentWindow[fname]) {
            frame.contentWindow[fname](obj);
            return true;
        }
    }

    if (!callIfPossible($('#iFM')[0])) {
        $('.TabContent', $('#iFM')[0].contentWindow.document).each(function () {
            callIfPossible(this);
        });
    }
}

Ultimately, you should be able to do is just something like

function Content_chkClick(obj) {
    Content_xClick("Content_chkClick", obj);
}
function Content_invokeClickEvent(id) {
    Content_xClick("Content_invokeClickEvent", id);
}
Sign up to request clarification or add additional context in comments.

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.