admin管理员组

文章数量:1321836

The website uses onLoad to load a JavaScript file (.js). I want to change one of the functions inside that js file using JavaScript.

The function is called "Hello13" and is among many other functions (hello5, hello6, etc..)

The Hello13 function looks something like this:

function hello13(x, y, z) {
    if (x == null) {
        //something, something.
    }
    if (y == null) {
        //something else, something else.
    }
    if (z == null) {
        //something diffrent, something diffrent.
    }
}

So as you can see, when the function is called it is given some values (x, y, z).

I want to "hard code" the z to be my own value (inside the js file).

So it would be something like this after i have Programmatically changed it:

function hello13(x, y, z) {
    z = MyValueOverride.
.....

How can i add my own "z" value inside the js file, After the site has loaded, using JavaScript?

The website uses onLoad to load a JavaScript file (.js). I want to change one of the functions inside that js file using JavaScript.

The function is called "Hello13" and is among many other functions (hello5, hello6, etc..)

The Hello13 function looks something like this:

function hello13(x, y, z) {
    if (x == null) {
        //something, something.
    }
    if (y == null) {
        //something else, something else.
    }
    if (z == null) {
        //something diffrent, something diffrent.
    }
}

So as you can see, when the function is called it is given some values (x, y, z).

I want to "hard code" the z to be my own value (inside the js file).

So it would be something like this after i have Programmatically changed it:

function hello13(x, y, z) {
    z = MyValueOverride.
.....

How can i add my own "z" value inside the js file, After the site has loaded, using JavaScript?

Share Improve this question asked Jan 27, 2013 at 0:50 Aleksander AziziAleksander Azizi 9,8779 gold badges61 silver badges88 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If it's a globally defined function in some javascript that is loaded via an inline script tag (either locally defined or in an external JS file), you can just redefine it with your own version of that function. Whichever definition es last will be the active one.

So, it sounds like what you want to do is to provide your own definition right after the external JS file containing the target function. That way your definition will replace the original one. If you want to retain the original one, you can assign it to another variable name before replacing it.

// save original function reference (if desired)
var originHello13 = hello13;

// now define the new function which will replace the previous definition
function hello13(x, y, z) {
    z = MyValueOveride...
}

If the external javascript file is dynamically loaded, then things are a little more plicated because you will need to figure out how to know when the dynamically loaded external JS file has been loaded so you can then define your override. You'd have to show us more about how the external JS file is loaded for us to offer specifics on how to do that.

Redefine it. If you want to use the logic in the original function, store the reference to the original function and then redefine it and call the original from the new one:

var originalHello13 = hello13;
hello13 = function( x, y, z ){
    z = MyValueOverride.
    return originalHello13( x, y, z );
}

Create a closure that redefines the function, then call the old function whenever your new function is called:

hello13 = (function () {
    // Keep a reference to the old function
    var _hello13 = hello13;

    // return a new function, which will be stored in hello13
    return function (x, y, z) {
        z = MyValueOverride;
        return _hello13(x, y, z);
    };
}());

Here's the fiddle: http://jsfiddle/hKavH/

本文标签: Programmatically changeedit JavaScript function using JavaScriptStack Overflow