Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Override Javascript function

Posted on 2014-04-08
13
Medium Priority
?
5,187 Views
Last Modified: 2014-04-09
I have a javascript file that contains a function, "doSubmitTab" which is called throughout my application.  

However, on one page, I would like to override the function to simply alert the user...something like "Action is Denied".


What is the javascript syntax to override the the "doSubmitTab" function?
0
Comment
Question by:bmsande
  • 6
  • 5
  • 2
13 Comments
 
LVL 23

Expert Comment

by:Snarf0001
ID: 39987373
As long as you can declare the override AFTER the original in code, it will replace it.
JavaScript functions override the same declaration.

    <script>

        function DoStuff() {
            alert("foo");
        };

        function DoStuff() {
            alert("bar");
        };

        DoStuff();      //will alert "bar" only

    </script>

Open in new window

0
 

Author Comment

by:bmsande
ID: 39987384
So just declare a function of the same name, or is there a special override syntax?
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 39987397
Nope, same name.
If you have this wrapped in a javascript class or something with .prototype declarations, it's a different matter.
But if you have a simple function declaration as show above, then just duplicate the function name.  Again, just make sure it ends up rendering underneath the original.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bmsande
ID: 39987402
sorry the code above didnt display on my mobile, i'll give that a shot.
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 39988611
No this is not the solution.
As you have written new function, it is always get called, even for earlier calls also.

so better way if you can update existing method then update it like this:

$(function(){    
    callme('a');
    callme();
});

function callme(testVariable)
{
    if(typeof(testVariable) == "undefined")
    {
        alert('without parameter method called');
    }
    else
    {
         alert('with parameter method called');   
    }
}

Open in new window


Hope helps you.
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 39988621
I think that was the point... on one specific page he wants to override all calls of the function.
So on one page, declare the function again and it will override all calls.
0
 

Author Comment

by:bmsande
ID: 39988861
So as my luck would have it, the .js file which contains the function I want to override is declared towards the end of the page.  The part of the page I have control over is above the declaration...

Is it possible to override?
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 39988938
Hi,
you just need to check page structure. I mean whether you are using master page or user control, etc.
Please share page structure.

And this is correct if that page, from which you have to call same function for different behavior and same page do not contains old function definition then you can write same function definition on that same page with same function name.
In this case it will call function which is declared on the same page.

Please share your comments.
0
 

Author Comment

by:bmsande
ID: 39988949
I have no idea what page structure, master page, or user control is.

All I know is that I created a very simple function of the same name as the function I want to override and it had no effect.  The original function is still called.  I assumed that since the .js file which contained the original function was declared AFTER my override function, the order of operation was incorrect.
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 39988985
Correct.  The .js file with the original function MUST be before your override.
0
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 2000 total points
ID: 39989005
Are you using jquery?  You can still do the override if you wait for the doc to finish loading and then reset it based on the window.<functionname>

    <script>
        $(document).ready(function () {
            window.DoStuff = function () {
                alert("bar");   //this is the override
            };
        });
    </script>

    <script>
        function DoStuff() {
            alert("foo");   //this is the original, occuring later in the page
        };
    </script>

Open in new window

0
 

Author Comment

by:bmsande
ID: 39989196
This worked beautifully!!!!  Thank you!!!!
0
 

Author Closing Comment

by:bmsande
ID: 39989200
I used the microsoft jquery and plugged in my function into the code provided and it worked on first try!

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question