Duplicate function name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • man.shahi@gmail.com

    Duplicate function name

    Hi all,

    i have a html page with some javascript functions in a js file linked
    to it.
    now, if i create a function in the html page with the same name as one
    of js functions, how can i call the function that exists on js file
    (from html)?

    regards,
    man shahi

  • Martin Honnen

    #2
    Re: Duplicate function name



    man.shahi@gmail .com wrote:

    [color=blue]
    > i have a html page with some javascript functions in a js file linked
    > to it.
    > now, if i create a function in the html page with the same name as one
    > of js functions, how can i call the function that exists on js file
    > (from html)?[/color]

    A second function declaration for the same function name overwrites an
    earlier one.

    --

    Martin Honnen

    Comment

    • Randy Webb

      #3
      Re: Duplicate function name

      man.shahi@gmail .com wrote:[color=blue]
      > Hi all,
      >
      > i have a html page with some javascript functions in a js file linked
      > to it.
      > now, if i create a function in the html page with the same name as one
      > of js functions, how can i call the function that exists on js file
      > (from html)?[/color]

      Rename the one in your html page or in your .js file, but you have to
      rename one of them.

      --
      Randy
      comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

      Comment

      • Daniel Kirsch

        #4
        Re: Duplicate function name

        man.shahi@gmail .com wrote:[color=blue]
        > i have a html page with some javascript functions in a js file linked
        > to it.
        > now, if i create a function in the html page with the same name as one
        > of js functions, how can i call the function that exists on js file
        > (from html)?[/color]

        You may copy the whole function:

        // this one might be in you linked js file
        function foo() {
        alert("foo1");
        }

        // copy the function
        var foo_old = foo;

        // overwrite the existing function. Do it within another function
        // as otherwise the parser might have overwritten foo before
        // any execution
        function tmp() {
        window.foo = function() {
        if (foo_old)
        foo_old();
        alert("foo2");
        }
        }
        // call that function now
        tmp();

        // call the overwritten function
        foo();


        If you use objects with prototype, this might be easier:

        function o() {
        }

        o.prototype.foo = function() {
        alert("foo1");
        };

        o.prototype.foo _old = o.prototype.foo ;

        o.prototype.foo = function() {
        if (this.foo_old)
        this.foo_old();
        alert("foo2");
        };

        new o().foo();

        Daniel

        Comment

        Working...