gpt4 book ai didi

javascript - Monkey Patch jquery 函数内容

转载 作者:行者123 更新时间:2023-12-03 11:17:44 27 4
gpt4 key购买 nike

预先感谢您的帮助。

我正在尝试对现有的 javascript 函数进行猴子修补,以便其其中一行指向新位置。重新定义该函数很容易,只不过它是从包含动态内容的服务器端代码呈现的。

function GoPrint() {
$.cookie('edit_child','on',{expires:28,path:'/'}); //Dynamically created (edit child could be off)
window.open('../../Common/Output/HTMLtoPDF.aspx','print'); //Always static, need to change this call.
}

在我的示例中,创建 cookie 的第一行是在服务器端动态创建的,因此该属性可以设置为关闭。

我需要更改 window.open 以调用不同的页面而不是 htmltopdf 页面。

虽然很讨厌,但我想重新定义该函数,替换 HTMLtoPDF 文本以指向新页面。

我已经在下面开始了这个,但不知道如何获取函数的现有内容来更改它。

function($){

var _oldPrint = $.fn.GoPrint;

$.fn.GoPrint = function(arg1,arg2){
return _oldPrint.call(this,'',);
};
})(jQuery);

有什么建议吗?

最佳答案

一种方法是在旧函数上调用 toString,将旧 URL 替换为新 URL,然后评估结果,但前提是要考虑安全隐患。

纯粹从安全 Angular 来看,更安全的方法是在 GoPrint 的猴子补丁中对 window.open 函数进行猴子补丁。

function($) {

var _oldPrint = $.fn.GoPrint;

$.fn.GoPrint = function(arg1, arg2) {
var _oldopen = window.open;

window.open = function() {
_oldopen.call('YOUR_URL_HERE', 'print');
};

return _oldPrint.call(this);

window.open = _oldopen;
};
})(jQuery);

关于javascript - Monkey Patch jquery 函数内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27261250/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com