使用jquery创建新窗口

Mar*_*ner 7 javascript jquery

我正在使用JavaScript/jQuery开发一个小项目.

为了显示在javascript中完成的计算结果,我想打开一个带有一些预定义内容的新窗口并修改此内容以显示结果:我使用如下代码:

var resultwindow = window.open('result.html')
var doc = $('body', resultwindow.document);
doc.append("<p>Result</p>")
Run Code Online (Sandbox Code Playgroud)

这是行不通的,因为当我附加内容时结果文档尚未加载,因此它被'result.html'的内容覆盖.

我也试过了

$(resultwindow.document).ready(function() {
    // ... Fill result document here
})
Run Code Online (Sandbox Code Playgroud)

$(resultwindow.document).load(function() {
    // ... Fill result document here
})
Run Code Online (Sandbox Code Playgroud)

ready()仅适用于当前文档(如果当前文档已加载则立即调用它),并且load根本不会被调用.

也许有人可以指出我正确的方向.提前致谢!

编辑:

我终于通过在Javascript中"手动"创建新文档来解决这个问题:

w = window.open('','newwinow','width=800,height=600,menubar=1,status=0,scrollbars=1,resizable=1);
d = w.document.open("text/html","replace");
d.writeln('<html><head>' +
  '<link rel="stylesheet" type="text/css" href="style.cs"/></head>' +
  +'<body></body></html>');
// use d to manipulate DOM of new document and display results
Run Code Online (Sandbox Code Playgroud)

如果我今天要做同样的事情(两年后的经验),我会使用像Handlebars这样的Javascript模板库来维护模板并将其编译为javscript.

Har*_*arv 0

将数据发送到查询字符串中的 result.html,然后让 result.html 显示那里的数据。如果您不想让它变得不那么明显,您可以对查询字符串中的数据进行哈希处理,然后让结果页面对其进行去哈希处理。