Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Override Javascript function

Posted on 2014-04-08
13
Medium Priority
?
5,064 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
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!

 

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

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!

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

688 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