1

How would I go about removing a function if a certain browser is detected ? Here is the function and the detection:

function parallaxIt(e, target, movement) {
  var $this = $("#app");
  var relX = e.pageX - $this.offset().left;
  var relY = e.pageY - $this.offset().top;

  TweenMax.to(target, 1, {
    x: (relX - $this.width() / 2) / $this.width() * movement,
    y: (relY - $this.height() / 2) / $this.height() * movement,
    z: 0.01,
    rotation:0.01
  });
}


var version = detectIE();
if (version === false) {
  //Do Nothing
} else if (version >= 12) {
    //Remove Function
} else {
    $("#iefix").attr({href : "/static/css/app-iefix.css"});
    //Remove Function
}

So when any IE is detected I want to remove the function parallaxIt() or break it so It doesn't work any ideas? Kind Regards

(the detect part of the code is obviously a small snippet of the full code so it's easier for you folks to read instead of going through the full code)

6
  • 1
    Just don't call it in the first place if IE is being used.
    – Quentin
    Jan 2, 2019 at 12:50
  • It triggers on window.onload Jan 2, 2019 at 12:51
  • 2
    So change the window.onload function so it tests if the browser is IE first!
    – Quentin
    Jan 2, 2019 at 12:52
  • I know I can add parallaxIt() in else if and else of the detection but I would still want to know how to remove a function all together Jan 2, 2019 at 12:56
  • 1
    Delete all references to it. How you do that depends on where the references are. It's entirely possible that you end up generating Reference Errors because the function no longer exists but other code still tries to call it. This is one reason the "delete the function" approach is a bad one. It's massively over complicated.
    – Quentin
    Jan 2, 2019 at 12:56

1 Answer 1

6

At least for me in Chrome v71 (Windows 10), deleting the function does not do anything (it keeps existing). But you can re-assign it to become a no-op function:

function parallaxIT() {
  console.log("inside parallaxIT");
}

// delete
delete window.parallaxIT;
console.log("parallaxIT was 'deleted', does it still show output:");
parallaxIT();

console.log("=====");

// re-assign
window.parallaxIT = function() {}    // no-op function
console.log("parallaxIT was re-assigned, does it still show output:");
parallaxIT();

console.log("=====");

1
  • The delete should be placed in a try ... catch block, because if you are attempting to do this in Firefox, it throws an error.
    – Robidu
    Jan 18 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.