使用JavaScript以编程方式更改/编辑JavaScript功能 [英] Programmatically change/edit JavaScript function using JavaScript

查看:71
本文介绍了使用JavaScript以编程方式更改/编辑JavaScript功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该网站使用 onLoad 加载 JavaScript 文件( .js )。
我想使用JavaScript更改该js文件中的一个函数。


$ b 该函数被称为 Hello13 ,并且是许多其他函数( hello5 hello6 等等。)



Hello13 函数看起来像这样:

  function hello13(x,y,z){
if(x == null){
// something,something。
}
if(y == null){
//别的东西,别的东西。
}
if(z == null){
// something diffrent,something diffrent。






$ b

正如你所看到的,当函数被调用时它给了一些值( x,y,z )。 我想要硬代码z是我自己的值(在js文件中)。

所以在我编程改变它之后会是这样的: p>

 函数hello13(x,y,z){
z = MyValueOverride。
.....

如何添加我自己的 z 在js文件里面的值,在网站加载之后,使用JavaScript?

解决方案

如果它是通过内联脚本标记(本地定义的或外部JS文件)加载的某个JavaScript中的全局定义函数,则可以使用该函数的自己版本重新定义它。无论哪个定义最后都会是活动定义。



所以,您想要做的就是在包含目标的外部JS文件之后提供自己的定义功能。这样你的定义将取代原来的定义。如果你想保留原来的一个,你可以在替换之前将它分配给另一个变量名。

  //保存原始函数参考(如果需要的话)
var originHello13 = hello13;

//现在定义新函数,它将取代先前的定义
函数hello13(x,y,z){
z = MyValueOveride ...
}

如果外部JavaScript文件是动态加载的,则事情会更复杂一些,因为您需要了解如何知道动态加载的外部JS文件何时被加载,然后才能定义覆盖。您必须向我们展示更多关于外部JS文件是如何加载给我们的,以提供关于如何实现这一点的具体信息。


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?

解决方案

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 comes 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 complicated 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.

这篇关于使用JavaScript以编程方式更改/编辑JavaScript功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆