试图使用新窗口的控制台 [英] trying to use console of a new window

查看:109
本文介绍了试图使用新窗口的控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者学习JS而且我坚持使用以下功能。

I'm beginner to learn JS and I am stuck with the following function.

我正在尝试打开一个新窗口并在控制台中打印新窗户。
我的代码如下:你能否告诉我哪里出错?

I'm trying to open a new window and print in the console of the new window. My code is as follows:Can you please tell me where's the error ?

var newWindow = window.open("url");
newWindow.onload = function (newWindow) {
  newWindow.console.log("...something...");
}


推荐答案

你想得到的结果从代码是不可能的。让我们调用你执行javascript window 1 的窗口,然后打开一个全新的窗口 window 2 。现在,窗口2 既没有引用你的js代码,也没有 onload 事件。 窗口2 的onload事件在窗口1 实例中触发。因此,您将在窗口1获取控制台日志实例不在窗口2

The result you want to get from the code is not possible. Let's call the window where you are executing javascript window 1 and you are opening a fresh new window window 2. Now, window 2 neither has reference of your js code nor has the onload event. onload event of window 2 is firing at window 1 instance. So, you are getting the console log at window 1 instance not in window 2.

请参阅小提琴控制台窗口1 instance。

See the fiddle's console for window 1 instance.

这篇关于试图使用新窗口的控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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