2012-11-13 26 views
2

目前,我这样写代码 -如何在打开之前填充新窗口?

chatWindow = window.open("chatWindow.html", "Chat Window", 
resizable=0,width=700,height=600); 

var currentUserElement = chatWindow.document.createElement("currentUserElement"); 
    currentUserElement.setAttribute("id", "currentUserElement"); 
    currentUserElement.setAttribute("value",currentUserName); 

然而,有一个方法可以让我先做的createElement()部分调用之前对window.open()?

+1

让服务器首先创建它。 :) – epascarello

回答

3

我只看到如何在客户端上做到这一点的唯一方法就是使用blob。 HTML:

<input type="text" id="textId"></input> 
<button id="buttonId">Open new Window</button> 

而且JS:

var button = $("#buttonId"); 

button.click(function() { 
    var text = $("#textId").val(); 
    var html = "<html>" 
     html += "<head><title>Window: " + text + "</title></head>"; 
     html += "<body onLoad='javascript:alert(\"" + text + "\")'>"; 
     html += text 
     html += "</body></html>"; 
    var blob = new Blob([html], {type: 'text/html'}); 
    window.open(window.URL.createObjectURL(blob)); 
});​ 

我创建了一个jsfiddle来说明这种方法。正如你所看到的JS也将被执行。关于使用这种技术的html5rocks的屏幕共享有一个有趣的article