gpt4 book ai didi

javascript - 区分 console.log 和两个 iframe

转载 作者:行者123 更新时间:2023-12-02 16:21:32 25 4
gpt4 key购买 nike

我有一个页面(我们称之为主页),其中有两个 IFrame 显示同一页面(我们称之为子页面)。

如果我在子页面的 javascript 代码中编写 console.log('hello') ,当我加载主页时,我会在控制台中收到两条相同的消息 hello.

有没有办法区分日志来自哪个 IFrame?或者,换句话说,我如何了解日志来自两个 IFrame 中的哪一个?

最佳答案

您可以通过在网址中添加哈希来区分它们,例如:

<iframe src="child.html#iframe-1"></iframe>
<iframe src="child.html#iframe-2"></iframe>

然后,在您的子页面中,覆盖 console.log 函数,以便它添加该哈希作为前缀:

if(window.console && console.log){
var old = console.log;
console.log = function(){
Array.prototype.unshift.call(arguments, window.location.hash + ': ');
old.apply(this, arguments)
}
}
console.log('hi'); // Will output '#iframe-X: hi'

覆盖控制台的代码来自 Arun P Johny .

关于javascript - 区分 console.log 和两个 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29066008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com