将内容添加到新打开的窗口

新手上路,请多包涵

我不知道如何解决这个问题,我试过阅读很多帖子但没有人回答。

我需要打开一个新窗口,其中包含一个已经编码的页面(在同一域内)并添加一些内容。

问题是,如果我使用 OpenWindow.write() 页面尚未加载,或者它覆盖了所有内容,并且只显示通过写入添加的代码。

 var OpenWindow = window.open('mypage.html','_blank','width=335,height=330,resizable=1');
OpenWindow.document.write(output);

output 是我需要附加的代码。

我需要它至少在 Firefox、IE 和 GC 上工作。

提前致谢。如果我需要使用 JQuery,这不是问题。

原文由 Mario Corchero 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 328
2 个回答

parent .html 中:

 <script type="text/javascript">
    $(document).ready(function () {
        var output = "data";
        var OpenWindow = window.open("child.html", "mywin", '');
        OpenWindow.dataFromParent = output; // dataFromParent is a variable in child.html
        OpenWindow.init();
    });
</script>

child.html 中:

 <script type="text/javascript">
    var dataFromParent;
    function init() {
        document.write(dataFromParent);
    }
</script>

原文由 Ray Cheng 发布,翻译遵循 CC BY-SA 3.0 许可协议

当您想打开新标签页/窗口时(取决于您的浏览器配置默认值):

 output = 'Hello, World!';
window.open().document.write(output);

例如,当输出是 Object 并且您想要获取 JSON(也可以生成任何类型的文档,甚至是用 Base64 编码的图像)

 output = ({a:1,b:'2'});
window.open('data:application/json;' + (window.btoa?'base64,'+btoa(JSON.stringify(output)):JSON.stringify(output)));

更新

谷歌浏览器 (60.0.3112.90) 阻止此代码:

Not allowed to navigate top frame to data URL: data:application/json;base64,eyJhIjoxLCJiIjoiMiJ9

当您想将一些数据附加到现有页面时

output = '<h1>Hello, World!</h1>';
window.open('output.html').document.body.innerHTML += output;

output = 'Hello, World!';
window.open('about:blank').document.body.innerText += output;

原文由 iegik 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题