覆盖jQuery的.load函数 [英] Override .load function of jQuery

查看:89
本文介绍了覆盖jQuery的.load函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个站点在300多个页面中使用$(selector).load(path)函数.现在,我的客户的要求已更改,我需要访问跨域才能调用这些页面.

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.

为此,我必须借助YQL将所有.load(函数替换为某些跨域函数.

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

是否可以覆盖我的.load函数并调用阻止默认值并执行我自己的代码?

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

推荐答案

没有干净的方法可以做到这一点,特别是因为$.fn.load根据参数执行不同的操作,并且替换它会影响所有这些子功能.

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.

但是,jQuery 支持AJAX钩子,您也许可以实现所需的目标.

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

如果您只需要支持IE的XDomainRequest,请查看此插件: https://github.com/jaubourg/ajaxHooks/blob/master/src/ajax/xdr.js

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

无论如何,如果您真的想替换jQuery的ajax加载功能,则此代码应做到:

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
}

这与jQuery用于确定某人是否要绑定onload事件或执行AJAX加载的检查完全相同.

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

这篇关于覆盖jQuery的.load函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆