1

I want to do this, because I am working with third-party scripts releated to Internet Marketing. Some of them may contain jQuery library included within and it interfere with recent or latest jQuery Library that is included on my website.

Hence, I'd like to switch between .on() and .bind() dynamically upon website load with a help of variable.

For example, let's say I have global variable:

var incJS = false;

And now depending of the third-party script, I know if they have older Lib included so I'd use this.

function tpNetwork ()
{
    incJS = true;

    startGateway('XXXXX');

    $('#fancybox-outer')
        .delay(1000)
        .fadeTo(300, 1);

    preventGTW();
    widgetStyle();
}

Now as you may see there's a function at the very bottom widgetStyle()

That function contain loads of things, but important part is following:

$(window).on('resize', function () {
    if ($('.widget_wrap').length) widgetCenter_horizontal();
});

It has .on() method there. That is not supported in very old jQuery that's been used by that third-party network. I'd like to switch every single .on() with .bind() but I don't know how to, without duplicating things. I did it like this, but it's duplicate and I believe there's easier way.

if (!incJS)
{
    $(window).on('resize', function () {
        if ($('.widget_wrap').length) widgetCenter_horizontal();
    });
}
else
{
    $(window).bind('resize', function () {
        if ($('.widget_wrap').length) widgetCenter_horizontal();
    });
}

Any kind of tips/help is appreciated. I am really out of any ideas and by doing researches I found nothing.

4

2 回答 2

3

关于什么:

$(function(){
    if(!$.fn.on) $.fn.on = $.fn.bind;
});

当然,这不包括任何委托支持。

演示

于 2013-08-07T13:38:30.340 回答
0

好吧,感谢@smerny 的评论和@roasted 的回答,我做到了这一点,它解决了我的问题。感谢您提供所有提示和答案!

$.fn.onbind = function () {
    if (!ajaxGateway)
        $.fn.onbind = $.fn.on;
    else
        $.fn.onbind = $.fn.bind;
};

现在一切都如我所愿。这是一个例子。

$(window).onbind('resize', function () {
    if ($('.widget_wrap').length) widgetTop();
});
于 2013-08-07T15:35:40.450 回答