繁体   English   中英

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

[英]Add content to a new open window

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

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

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

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

output是我需要附加的代码。

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

如果我需要使用 JQuery,这不是问题。

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

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;

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>

这是你可以尝试的

  • 在mypage.html里写一个函数说init()做html的事情(追加或者什么)
  • 而不是OpenWindow.document.write(output); 当dom准备就绪时调用OpenWindow.init()

所以父窗口会有

    OpenWindow.onload = function(){
       OpenWindow.init('test');
    }

在孩子身上

    function init(txt){
        $('#test').text(txt);
    }

在页面加载后调用document.write ,将删除所有内容并将其替换为您提供的参数。 而是使用DOM方法添加内容,例如:

var OpenWindow = window.open('mypage.html','_blank','width=335,height=330,resizable=1');
var text = document.createTextNode('hi');
OpenWindow.document.body.appendChild(text);

如果你想使用jQuery,你可以获得一些更好的API来处理。 例如:

var OpenWindow = window.open('mypage.html','_blank','width=335,height=330,resizable=1');
$(OpenWindow.document.body).append('<p>hi</p>');

如果您需要在新窗口的DOM准备就绪后运行代码,请尝试:

var OpenWindow = window.open('mypage.html','_blank','width=335,height=330,resizable=1');
$(OpenWindow.document.body).ready(function() {
    $(OpenWindow.document.body).append('<p>hi</p>');
});

如果要打开发送数据POST或GET方法的页面或窗口,可以使用如下代码:

$.ajax({
    type: "get",  // or post method, your choice
    url: yourFileForInclude.php, // any url in same origin
    data: data,  // data if you need send some data to page
    success: function(msg){
                console.log(msg); // for checking
                window.open('about:blank').document.body.innerHTML = msg;  
               }
}); 

更简单!

只需将下面的代码放在代码的<head> </head>之间,所有链接都会在新窗口中打开:

<base target="_blank">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM