Solved

Override Javascript function

Posted on 2014-04-08
13
4,271 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 22

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 22

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
 

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 22

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 22

Expert Comment

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

Accepted Solution

by:
Snarf0001 earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now