用另一个替换做document.write的函数 [英] Replace a function doing document.write with another

查看:102
本文介绍了用另一个替换做document.write的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本标签,其中函数A()执行document.write()。我不能访问A(),因为它来自第三部分脚本。

I have following script tag where a function A() does a document.write(). I don't have access to A() since it's from third part script.

//block1
<script type="text/javascript">
    A();
</script>

我没有任何id / class hook来阻止,但我可以在A之前引​​入一个函数调用()这样。

I don't have any id/class hook to block1 but I can introduce a function call before A() like this.

<script type="text/javascript">
     B();
     A();
</script>

我想要B()函数将block1替换为

I want B() function to replace block1 to

<script type="text/javascript">
     C();
</script>    
// or keep as is
<script type="text/javascript">
     A();
</script>

这是可能的,我该怎么办?

Is this possible and how should I go about it?

推荐答案

如果您可以在 A()之后插入代码,但在调用之前(可能无法进行) ),那么你可以重新定义 A(),就像在JavaScript中定义任何其他函数一样。

If you can insert code after A() is defined, but before it is called (might not be possible), then you can redefine A() like you would define any other function in JavaScript.

不可能,您可以在 A()被调用之前重新定义 document.write ,然后在 A()被调用。例如:

If that is not possible, you can redefine document.write before A() gets called, and then undo that after A() gets called. For example:

var write = document.write;
document.write = function(s)
{
    //do nothing, or whatever you want
    console.log(s);
}
//then, after A(), just do:
document.write = write;

这篇关于用另一个替换做document.write的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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