Sashi Kant
Sashi Kant

Reputation: 13465

Bind one more javascript method to the href in Anchor tag

I have a web page, where in onLoad a list of anchor tags are created using jQuery (external javascript). (href and onclick events are also defined there).

For example:

<a href="javascript:method1(1)" onclick="return method2(1);">Test1</a>
<a href="javascript:method1(2)" onclick="return method2(2);">Test2</a>

Since it is created through external javascript file, I cannot edit this onLoad, neither the method definition of method1

Now I want method3 to be called after medthod1 is completed.

Any help will be appreciated.

Upvotes: 0

Views: 363

Answers (2)

Amir Sherafatian
Amir Sherafatian

Reputation: 2083

you can overwrite method2, after your external js create your anchors, you can use load event of your body tag, after all assets have been loaded:

function window_load()
{
     var newHandler = method2;
     method2 = function(arg){

         var result = newHandler(arg);

         // ...
         // you can call here any function or something else
         // ...

         return result;
     };
}

i tried to overwrite method2, by this approach you can change any method you want

Upvotes: 1

ckersch
ckersch

Reputation: 7687

You can write a wrapper function to call both:

<head>
<script>
    function wrapperFunction(arg1, arg2){
        method2(arg1);
        method3(arg2);
    }
</script>
</head>
<body>
<a href="javascript:method1(1)" onclick="wrapperFunction(1,2);">Test1</a>
...

Upvotes: 1

Related Questions