【发布时间】:2021-10-08 08:51:58
【问题描述】:
我的问题如下:是否可以将 .js 文件中的内容写入非链接 HTML 文件的控制台?
所以我有以下设置:当单击 html_1(index.html) 上的按钮时,它会打开一个新窗口,该窗口有一个名为 html_2(numero.html) 的 HTML。我想在弹出窗口打开时在 index.html 的控制台中写一些东西(使用 onload)。两个 HTML 文件都链接到各自独立的 .js 文件(index.js 和 numero.js)。我想从 numero.js 到 index.html 写入控制台,但它们没有链接。
这可能吗?
我现在拥有的。
HTML 索引
<button onclick="newWindow();" id="ventana" >ACIERTA NUMERO</button> 打开新窗口。
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="Stylesheet1.css">
<script type="text/javascript" src="objectosPredef.js"></script>
<title>Actividad 2</title>
</head>
<body onload="listaPropiedades()">
<div id="listaPropiedades"></div>
<input type="text" id="nombre" placeholder="entra tu nombre" />
<button onclick="crearCookie();">Click</button>
<button onclick="newWindow();" id="ventana" >ACIERTA NUMERO</button>
</body>
</html>
Index.js
function newWindow(url, title, w, h) {
var url = "numero.html";
var title = "";
var w = 300;
var h = 300;
var left = (screen.width / 2) - (w / 2);
var top = (screen.height / 2) - (h / 2);
return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
}
HTML 数字
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="numero.js"></script>
<title>count down</title>
</head>
<body onload="windowTimer()">
<div id="random"></div>
<div id="timer"></div>
</body>
</html>
Numero JS
function windowTimer() {
consoleLog();
var sec = 8;
setInterval(function () {
document.getElementById("timer").innerHTML = sec + " :segundos";
sec--;
if (sec == -1) {
window.close();
}
}, 1000);
}
function consoleLog() {
var x = Math.floor((Math.random() * 11));
document.getElementById("random").innerHTML = "Numero aleatorio: " + x;
}
目前它只是将随机数写入窗口,但它应该转到 index.html 的控制台日志。 我找不到任何关于此的内容。任何帮助表示赞赏。
【问题讨论】:
-
您可以使用 BroadcastChannel 之类的东西在父窗口和弹出窗口之间进行通信 - 并使用它来触发对控制台的写入?
标签: javascript html css console