onclick =“ window.open” + html吗?

时间:2018-06-20 17:57:57

标签: javascript html

我有一个故事情节文件,该文件正在通过索引文件中新窗口中的onclick事件打开。我有以下代码。

<div class="ease col-6 filterDiv interactives">
    <img class="display-b img-fluid" src="assets/images/digital-wellness-thumbnail.jpg" alt="alt">
        <a onclick="window.open('assets/storyline/digital-wellness/story.html', '_blank', 'location=yes,width=1024,height=768,scrollbars=yes,status=yes')">
            <div class="overlay">
            <i class="fa fa-plus-circle fa-lg text-overlay"></i>
            </div>
        </a>
    </div>

这将打开一个故事情节文件(在说“只编辑story.html文件”之前,我已经尝试过了,但是没有用),我想看看是否有一种方法可以在story.html文件在新窗口中打开。这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以为新窗口创建一个句柄,并通过向其发送带有postMessage() API的消息来修改其HTML。新窗口的页面将必须知道如何接受消息以及如何处理消息:

主页:

document.getElementById('my-link').addEventListener('click', function (event) {
  const newWindow = window.open('http://example.com/new-window.html');

  newWindow.postMessage('<div>Some HTML</div>', 'http://example.com/index.html');
});

新窗口页面:

window.addEventListener('message', receiveMessage, false);

function receiveMessage(event) {
  if (event.origin !== 'http://example.com/index.html') return;

  // Do something with event.message, which contains the HTML
}

请仔细阅读整个文档,因为它为如何启动和运行提供了很好的示例。


内联编辑:

<a onclick="javascript:var newWindow = window.open('assets/storyline/digital-wellness/
story.html','_blank', 'location=yes,width=1024,height=768,scrollbars=yes,status=yes');
newWindow.postMessage('<div>Some HTML</div>', '/index.html');">
    <div class="overlay">
        <i class="fa fa-plus-circle fa-lg text-overlay"></i>
    </div>
</a>