Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override .load function of jQuery

I have site which uses $(selector).load(path) function in more than 300 pages. Now my client's requirement has changed and I need to access cross domain to call these pages.

For the purpose I have to replace all the .load( function to some cross-domain function with the help of YQL.

Is it possible to override my .load function and call prevent default and do my own code?

like image 662
Khaled Javeed Avatar asked Dec 05 '25 19:12

Khaled Javeed


1 Answers

There is no clean way to do this, especially since $.fn.load does different things depending on the arguments and replacing it would affect all those subfunctions.

However, jQuery supports AJAX hooks which you might be able to achieve what you want.

In case all you need is support for IE's XDomainRequest, have a look at this plugin: https://github.com/jaubourg/ajaxHooks/blob/master/src/ajax/xdr.js


Anyway, if you really want to replace the ajax load function of jQuery, this code should do it:

var _load = $.fn.load;
$.fn.load = function(url, params, callback) {
    if(typeof url !== "string") {
        return _load.apply(this, arguments);
    }

    // do your ajax stuff here
}

This is exactly the same check jQuery uses to decide whether someone wants to bind the onload event or perform an AJAX load.

like image 92
ThiefMaster Avatar answered Dec 08 '25 09:12

ThiefMaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!