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?
3 Answers
Reset to default 4If 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
版权声明:本文标题:Programmatically changeedit JavaScript function using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742104527a2420958.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论