admin管理员组文章数量:1287541
I didn't know you could do this until I had banged my head against the wall on a troublesome bug and finally figured out we were failing because some jquery plugin had overwritten the escape function. So this will put up an alert and docwrite null:
escape = function(a){alert(a)}
document.write(escape("Need tips? Visit W3Schools!"));
Cool! (not).
Is there a way to restore the native escape function?
I didn't know you could do this until I had banged my head against the wall on a troublesome bug and finally figured out we were failing because some jquery plugin had overwritten the escape function. So this will put up an alert and docwrite null:
escape = function(a){alert(a)}
document.write(escape("Need tips? Visit W3Schools!"));
Cool! (not).
Is there a way to restore the native escape function?
Share Improve this question edited Mar 6, 2014 at 21:40 hugomg 70k29 gold badges164 silver badges255 bronze badges asked Jun 24, 2010 at 6:13 dfinnecydfinnecy 614 bronze badges 1- Thanks for the answers, it would be best to ditch the plugin or fix it but it is outside the scope I have control of. I think I will be able save the old escape and restore it. – dfinnecy Commented Jun 24, 2010 at 9:16
3 Answers
Reset to default 13Create an iframe and get the function from it:
function retrieveNative(native) {
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
var retrieved = iframe.contentWindow[native];
document.body.removeChild(iframe);
return retrieved;
}
window.escape = retrieveNative('escape');
Throw that plugin away because you don't know what evil lurks in there. If you can't do that, then the most optimal solution is to put up a var
keyword before escape so it doesn't leak to the global scope and stays within the plugin function.
$.fn.naughtyJQueryPlugin = function() {
var escape = function() { .. };
};
If you can't modify the plugin source, then wrap code around the plugin to hold on to a reference to the original escape, that you can fix later.
var origEscape = escape;
$.fn.naughtyJQueryPlugin = function() {
...
};
escape = origEscape;
Thanks to @Tim pointing out, here's a more robust technique if you can wrap around the plugin source:
(function(escape) {
$.fn.naughtyJQueryPlugin = function() {
// any change to escape (not window.escape) will affect the parameter
// we passed in the outer function to which we have a reference
// through a closure.
// This avoids manipulating the plugin's source, and still have both
// versions intact.
};
})(escape); // pass the global escape as a parameter
// should call the global escape
escape("hello");
// when plugin is called, overridden escape should be called
$("div").naughtyJQueryPlugin("message");
See example here. Both versions should co-exist peacefully.
If you can add your code before the plugin - you can "save" the original function:
oldescape = escape;
function fix() {
escape = oldescape;
}
本文标签: javascripta way to restore the native escape function after it has been overwrittenStack Overflow
版权声明:本文标题:javascript - a way to restore the native escape function after it has been overwritten? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741307532a2371459.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论