Solved

Override Javascript function

Posted on 2014-04-08
13
4,856 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

615 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