Solved

Override Javascript function

Posted on 2014-04-08
13
4,211 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

22 Experts available now in Live!

Get 1:1 Help Now