1

Here is the Superfish library used in Drupal. The superfish.js file at line 102 contains code:

$.fn.extend({
    hideSuperfishUl : function(){
        /* some statements */
    },
    showSuperfishUl : function(){
        /* some statements */
    }
});

I need to override these two functions. How to do it? (I don't mean how to do it in Drupal specifically, but rather how to do it in javascript)

P.S. Based on some information, I tried adding this code in my own script:

(function ($) {
  var orig_hideSuperfishUl = $.hideSuperfishUl;
    $.hideSuperfishUl = function(){
      alert('lol');
    }
})(jQuery);

Firebug shows that the statement starting with "var" runs once the page is refreshed, but the statement with "alert" does not run. Instead, the original hideSuperfishUl function runs.

[Edit 1] I changed the custom code to:

(function ($) {
  //var orig_hideSuperfishUl = $.hideSuperfishUl;
  var hideSuperfishUl = function(){
    alert('lol');
  }
})(jQuery);

[Edit 2] I added 4 breakpoints:

  1. in superfish.js at line 102 with code $.fn.extend({
  2. in superfish.js at line 106 with code o.retainPath = false;
  3. in my script at line with var hideSuperfishUl = function(){
  4. in my script at line with alert('lol');

After refreshing the page, code at breakpoint 1 runs first, then at breakpoint 3. After moving the mouse on and off menu, code at breakpoint 2 runs. Breakpoint 4 is not reached.

The call stack at breakpoint 1 is only two anonymous functions (the first is the outermost function with jQuery argument, the second is the breakpoint). Very similar call stack at breakpoint 3.

4
  • It may be an order of execution/race condition issue. try setting a breakpoint in the original implementation then check the callstack when the breakpoint is hit. You may need to change the order your JS is included/initialized or find a different approach to overriding. Commented Jul 23, 2013 at 19:14
  • I updated the question with information about four breakpoints and the call stack (in short, my script runs after superfish when the page refreshes). It looks correct. Unfortunately, I've no clue about different approaches to overriding (I found the current approach here). Commented Jul 23, 2013 at 20:03
  • What about breakpoint 2? The important questions there are: who is calling it? Is it being called via $.hideSuperfishUl() or had it been cached and called that way. If cached, was it cached prior to your overriding the function? etc... Commented Jul 23, 2013 at 20:16
  • Breakpoint 2 was called from lines 24 and 31 of superfish.js. But I figured it out already by trial and error. My custom override code should be $.fn.hideSuperfishUl = function(){(...)} instead of $.hideSuperfishUl = function(){(...)} or var hideSuperfishUl = function(){(...)}.That way it works. Commented Jul 23, 2013 at 21:57

2 Answers 2

1

The code which effectively overrides the function in question is:

$.fn.hideSuperfishUl = function(){(...)}
Sign up to request clarification or add additional context in comments.

Comments

0

What you're doing with

var orig_hideSuperfishUl = $.hideSuperfishUl;

is assigning the function to a new variable, which doesn't affect the original.

Try simply redefining the original:

var hideSuperfishUl = '';

or

var hideSuperfishUl = function() {...}

2 Comments

I commented the line var orig_hideSuperfishUl = $.hideSuperfishUl; and changed the override definition to var hideSuperfishUl = function(){(...)} but it still doesn't work (statements inside the function in my script are not called).
Then you probably have other issues, as dc5 mentioned above.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.