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)

user3112634
  • 523
  • 4
  • 10
  • 26
  • 1
    Just don't call it in the first place if IE is being used. – Quentin Jan 02 '19 at 12:50
  • It triggers on window.onload – user3112634 Jan 02 '19 at 12:51
  • 2
    So change the window.onload function so it tests if the browser is IE first! – Quentin Jan 02 '19 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 – user3112634 Jan 02 '19 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 02 '19 at 12:56
  • @Quentin I am guessing the OP is trying to prevent someone being able to type `parallaxIt()` in the browser console too. Not sure why the function can't just be placed within the `if` statement though... – Jamie Barker Jan 02 '19 at 13:45

1 Answers1

5

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("=====");
Peter B
  • 22,460
  • 5
  • 32
  • 69