I am working on a small project in JavaScript / jQuery.
To display the results of a calculation performed in javascript, I would like to open a new window with some predefined content and change this content to display the results: Im using this code:
var resultwindow = window.open('result.html') var doc = $('body', resultwindow.document); doc.append("<p>Result</p>")
This does not work because the resulting document is not yet loaded when the content is added, so it is overwritten with the contents of "result.html".
I also tried
$(resultwindow.document).ready(function() {
and
$(resultwindow.document).load(function() {
but ready() only works with the current document (it is called immediately if the current document is already loaded), and load is not called at all.
Maybe someone can point me in the right direction. Thanks in advance!
EDIT
I finally solved this by creating a new document "manually" in Javascript, for example:
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
If I did the same today (two years of experience later), I would use some Javascript template library, such as Handlebars , to save the template and compile it into javscript.
javascript jquery
MartinStettner
source share