tags:

views:

53

answers:

3

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?

+1  A: 
Anurag
In the latter case, that is likely to clobber the plug-in: it will redefine `escape` inside the function, then escape wuill be immediately reset to its original value, and then calls to `naughtyJQueryPlugin` will use the original escape function rather than the plug-in's version.
Tim Down
@Tim - thanks for the tip. A closure would probably better solve the problem. Updated answer.
Anurag
A: 

If you can add your code before the plugin - you can "save" the original function:

oldescape = escape;
function fix() {
    escape = oldescape;
}
Dror
+1  A: 

Create 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');
Alsciende
that's pretty clever.
dfinnecy